clear all
set more off
set mem 10000000
set matsize 10000

****************************************************************** 
*** Fuzzy Merge of Habitation Merge into Census Data *************
****************************************************************** 

** Set file paths
do "$path_code/paths.do"

********************************************************************************
********************************************************************************

** Step 1: Combine Census and RGGVY village-level datasets, and define RGGVY virtual plan
{
use "$pca/pca_census01_names.dta", clear
merge m:m st_code dt_code vi_code using "$vd01/vd_2001_names.dta", nogen
merge m:m st_code dt_code vi_code using "$pca/pca_census11_names.dta", nogen
merge m:m conc_id using "$conc/census_code_matches_names.dta", nogen ///
          keepusing(bk_code_conc01 village_conc01 bk_code_conc11 village_conc11 block_2001 block_2011)
merge m:m st_code dt_code vi_code using "$rggvy/rggvy_master_names.dta", nogen
merge m:m st_code dt_code vi_code using "$loh/rggvy_list_of_habs_names.dta", nogen

gen names_id = _n
joinby st_code dpr_code using "$rggvy/rggvy_district_progress_X_XI.dta", unmatched(both) _merge(merge_plan)
drop if merge_plan==2
duplicates t st_code-implement, gen(dup)
sort names_id si_no
foreach var of varlist sanction_cost-achiev_pct_BPL{
  egen temp1 = max(`var'), by(names_id)
  egen temp2 = min(`var'), by(names_id)
  replace `var' = temp1 if dup>0
  replace `var' = temp2 if dup>0 & temp1==.
  drop temp1 temp2
}
drop dup si_no
duplicates drop

// IMPORTANT NOTE: for defining virtual DPRs, the VD block code 2 (bk_code2_vd) is by far the most in agreement with RGGVY block code
// Even though PCA is our main dataset, there's very little discrepancy between PCA-VD village names, except in Meghalaya (which is a mess)
// So, basically, what RGGVY calls a block is really only reflected accurately (for all states) by the VD block (not tahsil) code!

gen stdt = st_code*100 + dt_code
gen stdtbk = stdt*10000+bk_code2_vd
replace stdtbk = stdt*10000+bk_code_rggvy if stdtbk==.
gen plan_num = .
replace plan_num = 10 if plan=="10th Plan"
replace plan_num = 11 if plan=="11th Plan"
egen plan_num2 = mode(plan_num), by(stdtbk)

gen dpr_diff = stdt-dpr_code
egen max_dpr_check1 = max(dpr_code) if dpr_code!=., by(stdt)
egen min_dpr_check1 = min(dpr_code) if dpr_code!=., by(stdt)
egen max_dpr_check2 = max(dpr_code) if dpr_code!=., by(stdtbk)
egen min_dpr_check2 = min(dpr_code) if dpr_code!=., by(stdtbk)
egen mode_dpr = mode(dpr_code) if dpr_code!=., by(stdtbk)
egen vplan_temp = mean(plan_num) if dpr_code!=., by(stdtbk)

gen vdpr_temp = .
replace vdpr_temp = dpr_code if  max_dpr_check1==min_dpr_check1 & dpr_code!=. & vdpr_temp==.
replace vdpr_temp = dpr_code if  max_dpr_check2==min_dpr_check2 & dpr_code!=. & vdpr_temp==.
replace vdpr_temp = mode_dpr if dpr_code!=. & vdpr_temp==.
replace vdpr_temp = min_dpr_check2 if dpr_code!=. & vdpr_temp==.
gen vdpr_temp2 = .
replace vdpr_temp2 = min_dpr_check2 if max_dpr_check2!=min_dpr_check2 & vdpr_temp!=min_dpr_check2 & dpr_code!=. 
replace vdpr_temp2 = max_dpr_check2 if max_dpr_check2!=min_dpr_check2 & vdpr_temp!=max_dpr_check2 & dpr_code!=. 
assert vdpr_temp!=. if dpr_code!=.

egen vplan     = mode(vplan_temp), by(stdtbk)
egen vplan2    = mode(plan_num2), by(stdtbk)
egen vplan3    = mode(vplan_temp), by(stdt)
replace vplan3 = vplan if vplan!=.
egen vdpr      = mode(vdpr_temp), by(stdtbk)
egen vdpr2     = mode(vdpr_temp2), by(stdtbk)
egen vdpr3     = mode(vdpr_temp), by(stdt)
replace vdpr3  = vdpr if vdpr!=.

assert dpr_code==. if vdpr==.
assert stdt==. if dpr_code!=. & vdpr==.
drop merge_plan *_temp* *dpr_check* dpr_diff mode_dpr implement sanction_cost award_cost-achiev_pct_BPL
compress
duplicates drop
duplicates drop names_id, force
save "$hmerge/pca_2001_names_merges_all.dta", replace
}

********************************************************************************
********************************************************************************

** Step 2: Prep 2003 Water Habitation survey for merge
{
insheet using "$hab3/final/final.csv", double comma clear
duplicates drop
tab state
replace state = subinstr(state,"&amp;","AND",.)
replace state = "JAMMU & KASHMIR" if state=="JAMMU AND KASHMIR"
replace state = "CHHATTISGARH" if state=="CHATTISGARH"
replace state = "UTTARAKHAND" if state=="UTTRANCHAL"
replace state = upper(trim(itrim(state)))
replace district = upper(trim(itrim(district)))
replace district = "KURUNG KUMAI" if district=="KURUNG KUMEY" & state=="ARUNACHAL PRADESH"
*replace district = "PASCHIM CHAMPARAN" if district=="PASHCHIM CHAMPARAN" & state=="BIHAR"
*replace district = "PURAV CHAMPARAN" if district=="PURBA CHAMPARAN" & state=="BIHAR"
replace district = "BASTAR" if district=="BASTER" & state=="CHHATTISGARH"
replace district = "LAHUL & SPITI" if district=="LAHUL AND SPITI" & state=="HIMACHAL PRADESH"
replace district = "BARAMULA" if district=="BARAMULLA" & state=="JAMMU & KASHMIR"
replace district = "PUNCH" if district=="POONCH" & state=="JAMMU & KASHMIR"
*replace district = "HAZARIBAG" if district=="HAZARIBAGH" & state=="JHARKHAND"
replace district = "KODARMA" if district=="KODERMA" & state=="JHARKHAND"
replace district = "PAKAUR" if district=="PAKUR" & state=="JHARKHAND"
replace district = "PASHCHIMI SINGHBHUM" if district=="PASCHIM SINGHBHUM" & state=="JHARKHAND"
replace district = "SARAIKELA KHARSAWAN" if district=="SAREIKELA AND KHARSAWAN" & state=="JHARKHAND"
replace district = "BANGALORE" if district=="BANGLORE URBAN" & state=="KARNATAKA"
*replace district = "CHAMRAJNAGAR" if district=="CHAMARAJANAGAR" & state=="KARNATAKA"
replace district = "DAKSHINA KANNADA" if district=="DAKSHIN KANNAD" & state=="KARNATAKA"
replace district = "DAVANAGERE" if district=="DAVANGERE" & state=="KARNATAKA"
replace district = "UTTARA KANNADA" if district=="UTTAR KANNADA" & state=="KARNATAKA"
replace district = "EAST NIMAR" if district=="EAST NIMAR KHARGONE" & state=="MADHYA PRADESH"
replace district = "NARSIMHAPUR" if district=="NARSINGHPUR" & state=="MADHYA PRADESH"
replace district = "EAST NIMAR KHARGONE" if district=="EAST NIMAR" & state=="MADHYA PRADESH"
*replace district = "FATEHGARHSAHIB" if district=="FATEHGARH SAHIB" & state=="PUNJAB"
replace district = "FIROZPUR" if district=="FEROZPUR" & state=="PUNJAB"
replace district = "KANCHEEPURAM" if district=="KANCHIPURAM" & state=="TAMIL NADU"
replace district = "KANNIYAKUMARI" if district=="KANNIYAKUMARI (HQ: NAGERCOIL)" & state=="TAMIL NADU"
replace district = "THOOTHUKKUDI" if district=="THOOTHUKUDI" & state=="TAMIL NADU"
replace district = "VILUPPURAM" if district=="VILLUPURAM" & state=="TAMIL NADU"
replace district = "THIRUVALLUR" if district=="TIRUVALLUR" & state=="TAMIL NADU"
replace district = "THIRUVARUR" if district=="TIRUVARUR" & state=="TAMIL NADU"
replace district = "THE NILGIRIS" if district=="NILGIRIS (HQ: UDHAGAMANDALAM)" & state=="TAMIL NADU"
replace district = "DHALAI" if district=="DHALAI DISTRICT" & state=="TRIPURA"
replace district = "BULANDSHAHAR" if district=="BULANDSHAHR" & state=="UTTAR PRADESH"
replace district = "KUSHINAGAR" if district=="KUSHI NAGAR" & state=="UTTAR PRADESH"
replace district = "MAHRAJGANJ" if district=="MAHARAJGANJ" & state=="UTTAR PRADESH"
replace district = "SIDDHARTHNAGAR" if district=="SIDDHARTH NAGAR" & state=="UTTAR PRADESH"
replace district = "JYOTIBA PHULE NAGAR" if district=="JYOTIBA PHOOLE NAGAR" & state=="UTTAR PRADESH"
replace district = "SANT KABIR NAGAR" if district=="SANT KABEER NAGAR" & state=="UTTAR PRADESH"
replace district = "JYOTIBA PHULE NAGAR" if district=="JYOTIBA PHOOLE NAGAR" & state=="UTTAR PRADESH"
*replace district = "PAURI GARHWAL" if district=="GARHWAL" & state=="UTTARAKHAND"
replace district = "BARDDHAMAN" if district=="BARDHAMAN" & state=="WEST BENGAL"
replace district = "DARJILING" if district=="DARJEELING" & state=="WEST BENGAL"
replace district = "HAORA" if district=="HOWRAH" & state=="WEST BENGAL"
replace district = "HUGLI" if district=="HOOGHLY" & state=="WEST BENGAL"
replace district = "KOCH BIHAR" if district=="COOCHBEHAR" & state=="WEST BENGAL"
replace district = "PURULIYA" if district=="PURULIA" & state=="WEST BENGAL"
replace district = subinstr(district,"24","TWENTY FOUR",1) if state=="WEST BENGAL"
replace district = subinstr(district,"PARAGANAS","PARGANAS",1) if state=="WEST BENGAL"
replace district = "EAST NIMAR" if district=="EAST NIMAR KHARGONE" & state=="MADHYA PRADESH"
replace district = "WEST NIMAR" if district=="KHARGONE" & state=="MADHYA PRADESH"
replace district = "PASHCHIMI SINGHBHUM" if district=="SARAIKELA KHARSAWAN" & state=="JHARKHAND"
replace district = "KURUNG KUMEY" if district=="KURUNG KUMAI" & state=="ARUNACHAL PRADESH"

{
replace block = upper(trim(itrim(block)))
replace block = subinstr(subinstr(subinstr(block,"*","",.)," -"," ",.),"- "," ",.)
replace block = subinstr(subinstr(subinstr(subinstr(block,"( ","(",.)," )",")",.),"-I"," I",.),"-1"," I",.)
replace block = subinstr(subinstr(subinstr(block,"(T)","",.),"(S.T)","",.),"(M)","",.)
replace block = subinstr(subinstr(subinstr(block,"C.D.BLOCK","",.),"SUB-DIV.","",.),"SUB-DIVISION","",.)
replace block = trim(itrim(subinstr(subinstr(subinstr(block,"(P)","",.),"CIRCLE","",.),"CD BLOCK","",.)))

replace block = "BAZARHATHNOOR" if block=="BAZARHATNOOR" & district=="ADILABAD" & state=="ANDHRA PRADESH"
replace block = "GUDIHATHNOOR" if block=="GUDIHATNOOR" & district=="ADILABAD" & state=="ANDHRA PRADESH"
replace block = "INDERAVELLY" if block=="INDERVELLY" & district=="ADILABAD" & state=="ANDHRA PRADESH"
replace block = "KAGAZNAGAR" if block=="KAGHAZNAGAR" & district=="ADILABAD" & state=="ANDHRA PRADESH"
replace block = "KOUTHALA" if block=="KOUTALA" & district=="ADILABAD" & state=="ANDHRA PRADESH"
replace block = "LOKESWARAM" if block=="LOKESHWARAM" & district=="ADILABAD" & state=="ANDHRA PRADESH"
replace block = "NENNAL" if block=="NENNEL" & district=="ADILABAD" & state=="ANDHRA PRADESH"
replace block = "REBBANA" if block=="REBBENA" & district=="ADILABAD" & state=="ANDHRA PRADESH"
replace block = "TANOOR" if block=="THANOOR" & district=="ADILABAD" & state=="ANDHRA PRADESH"
replace block = "WANKDI" if block=="WANKIDI" & district=="ADILABAD" & state=="ANDHRA PRADESH"
replace block = "BUKKARAYA SAMUDRAM" if block=="BUKKARAYASAMUDRAM" & district=="ANANTAPUR" & state=="ANDHRA PRADESH"
replace block = "NAMBULIPULIKUNTA" if block=="NAMBULAPULAKUNTA" & district=="ANANTAPUR" & state=="ANDHRA PRADESH"
replace block = "OBULADEVARACHERUVU" if block=="OBULADEVARECHERUVU" & district=="ANANTAPUR" & state=="ANDHRA PRADESH"
replace block = "TANAKAL" if block=="TANAKALLU" & district=="ANANTAPUR" & state=="ANDHRA PRADESH"
replace block = "VAJRAKARUR" if block=="VIJRAKARUR" & district=="ANANTAPUR" & state=="ANDHRA PRADESH"
replace block = "BANGARUPALEM" if block=="BANGARUPALYAM" & district=="CHITTOOR" & state=="ANDHRA PRADESH"
replace block = "BUCHINAIDU KANDRIGA" if block=="BUCHINAIDU KHANDRIGA" & district=="CHITTOOR" & state=="ANDHRA PRADESH"
replace block = "PEDDA THIPPASAMUDRAM" if block=="PEDDATHIPPA SAMUDRAM" & district=="CHITTOOR" & state=="ANDHRA PRADESH"
replace block = "PENUMURU" if block=="PENUMUR" & district=="CHITTOOR" & state=="ANDHRA PRADESH"
replace block = "PILERU" if block=="PILER" & district=="CHITTOOR" & state=="ANDHRA PRADESH"
replace block = "RAMACHANDRAPURAM" if block=="RAMACHANDRA PURAM" & district=="CHITTOOR" & state=="ANDHRA PRADESH"
replace block = "SODAM" if block=="SODUM" & district=="CHITTOOR" & state=="ANDHRA PRADESH"
replace block = "TIRUPATI (RURAL)" if block=="TIRUPATHI (RURAL)" & district=="CHITTOOR" & state=="ANDHRA PRADESH"
replace block = "YADAMARRI" if block=="YADAMARI" & district=="CHITTOOR" & state=="ANDHRA PRADESH"
replace block = "CHAKRAYAPET" if block=="CHAKARAYAPET" & district=="CUDDAPAH" & state=="ANDHRA PRADESH"
replace block = "CHAPAD" if block=="CHAPADU" & district=="CUDDAPAH" & state=="ANDHRA PRADESH"
replace block = "PENDLIMARRI" if block=="PENDLIMARRY" & district=="CUDDAPAH" & state=="ANDHRA PRADESH"
replace block = "PULLAMPETA" if block=="PULLAMPET" & district=="CUDDAPAH" & state=="ANDHRA PRADESH"
replace block = "SRI AVADHUTHA KASINAYANA" if block=="SRI AVADHUTH KASINAYANA" & district=="CUDDAPAH" & state=="ANDHRA PRADESH"
replace block = "T SUNDUPALLE" if block=="T.SUNDUPALLE" & district=="CUDDAPAH" & state=="ANDHRA PRADESH"
replace block = "CHILAKALURIPET H/O.PURUSHOTHA PATNAM" if block=="CHILAKALURIPET H/O.PURUSHOTHA PATNA" & district=="GUNTUR" & state=="ANDHRA PRADESH"
replace block = "NARASARAOPET" if block=="NARASARAOPETA" & district=="GUNTUR" & state=="ANDHRA PRADESH"
replace block = "VELDURTHI" if block=="VELDURTHY" & district=="GUNTUR" & state=="ANDHRA PRADESH"
replace block = "ELKATHURTHI" if block=="ELKATHURTHY" & district=="KARIMNAGAR" & state=="ANDHRA PRADESH"
replace block = "SIRCILLA" if block=="SIRSILLA" & district=="KARIMNAGAR" & state=="ANDHRA PRADESH"
replace block = "SULTANABAD" if block=="SULTHANABAD" & district=="KARIMNAGAR" & state=="ANDHRA PRADESH"
replace block = "ASWARAOPETA" if block=="ASWARAOPET" & district=="KHAMMAM" & state=="ANDHRA PRADESH"
replace block = "BURGAMPAHAD" if block=="BURGUMPAHAD" & district=="KHAMMAM" & state=="ANDHRA PRADESH"
replace block = "DAMMAPETA" if block=="DAMMAPET" & district=="KHAMMAM" & state=="ANDHRA PRADESH"
replace block = "JULURPAD" if block=="JULURPADU" & district=="KHAMMAM" & state=="ANDHRA PRADESH"
replace block = "MANUGURU" if block=="MANUGUR" & district=="KHAMMAM" & state=="ANDHRA PRADESH"
replace block = "PENUBALLI" if block=="PENUBALLE" & district=="KHAMMAM" & state=="ANDHRA PRADESH"
replace block = "THIRUMALAYAPALEM" if block=="TIRUMALAYAPALEM" & district=="KHAMMAM" & state=="ANDHRA PRADESH"
replace block = "VELAIRPADU" if block=="VELAIRPAD" & district=="KHAMMAM" & state=="ANDHRA PRADESH"
replace block = "VISSANNAPET" if block=="VISSANNAPETA" & district=="KRISHNA" & state=="ANDHRA PRADESH"
replace block = "PATTIKONDA" if block=="PATHIKONDA" & district=="KURNOOL" & state=="ANDHRA PRADESH"
replace block = "PEDDA KADUBUR" if block=="PEDDA KADABUR" & district=="KURNOOL" & state=="ANDHRA PRADESH"
replace block = "JADCHERLA" if block=="JADCHARLA" & district=="MAHBUBNAGAR" & state=="ANDHRA PRADESH"
replace block = "MAHBUBNAGAR" if block=="MAHABUBNAGAR" & district=="MAHBUBNAGAR" & state=="ANDHRA PRADESH"
replace block = "NARVA" if block=="NARWA" & district=="MAHBUBNAGAR" & state=="ANDHRA PRADESH"
replace block = "VEEPANGANDLA" if block=="WEEPANGANDLA" & district=="MAHBUBNAGAR" & state=="ANDHRA PRADESH"
replace block = "DOULTABAD" if block=="DOULTHABAD" & district=="MEDAK" & state=="ANDHRA PRADESH"
replace block = "KONDAPUR" if block=="KONDAPOOR" & district=="MEDAK" & state=="ANDHRA PRADESH"
replace block = "MULUG" if block=="MULUGU" & district=="MEDAK" & state=="ANDHRA PRADESH"
replace block = "MUNPALLE" if block=="MUNIPALLE" & district=="MEDAK" & state=="ANDHRA PRADESH"
replace block = "YELDURTHY" if block=="VELDURTHY" & district=="MEDAK" & state=="ANDHRA PRADESH"
replace block = "DAMERACHERLA" if block=="DAMARACHERLA" & district=="NALGONDA" & state=="ANDHRA PRADESH"
replace block = "DEVARAKONDA" if block=="DEVARKONDA" & district=="NALGONDA" & state=="ANDHRA PRADESH"
replace block = "NARAYANAPUR" if block=="NARAYANPUR" & district=="NALGONDA" & state=="ANDHRA PRADESH"
replace block = "NEREDCHERLA" if block=="NEREDUCHERLA" & district=="NALGONDA" & state=="ANDHRA PRADESH"
replace block = "PEDDAVOORA" if block=="PEDDAVURA" & district=="NALGONDA" & state=="ANDHRA PRADESH"
replace block = "OJILI" if block=="OZILI" & district=="NELLORE" & state=="ANDHRA PRADESH"
replace block = "BHIKNOOR" if block=="BHIKNUR" & district=="NIZAMABAD" & state=="ANDHRA PRADESH"
replace block = "KAMMARPALLE" if block=="KAMMAR PALLE" & district=="NIZAMABAD" & state=="ANDHRA PRADESH"
replace block = "NAGAREDDIPET" if block=="NAGAREDDYPET" & district=="NIZAMABAD" & state=="ANDHRA PRADESH"
replace block = "BESTAWARIPETA" if block=="BESTAVARIPETA" & district=="PRAKASAM" & state=="ANDHRA PRADESH"
replace block = "CHIMAKURTHI" if block=="CHIMAKURTHY" & district=="PRAKASAM" & state=="ANDHRA PRADESH"
replace block = "GIDDALUR" if block=="GIDDALURU" & district=="PRAKASAM" & state=="ANDHRA PRADESH"
replace block = "GUDLURU" if block=="GUDLUR" & district=="PRAKASAM" & state=="ANDHRA PRADESH"
replace block = "PEDA ARAVEEDU" if block=="PEDDA RAVEEDU" & district=="PRAKASAM" & state=="ANDHRA PRADESH"
replace block = "QUTUBULLAPUR" if block=="QUTHBULLAPUR" & district=="RANGAREDDI" & state=="ANDHRA PRADESH"
replace block = "YELAL" if block=="YALAL" & district=="RANGAREDDI" & state=="ANDHRA PRADESH"
replace block = "KOTABOMMALI" if block=="KOTABOMMILI" & district=="SRIKAKULAM" & state=="ANDHRA PRADESH"
replace block = "MELIAPUTTI" if block=="MELIAPUTTU" & district=="SRIKAKULAM" & state=="ANDHRA PRADESH"
replace block = "RANASTALAM" if block=="RANASTHALAM" & district=="SRIKAKULAM" & state=="ANDHRA PRADESH"
replace block = "DATTIRAJERU" if block=="DATHIRAJERU" & district=="VIZIANAGARAM" & state=="ANDHRA PRADESH"
replace block = "DHARMASAGAR" if block=="DHARAMSAGAR" & district=="WARANGAL" & state=="ANDHRA PRADESH"
replace block = "PARKAL" if block=="PARAKAL" & district=="WARANGAL" & state=="ANDHRA PRADESH"
replace block = "MOGALTHUR" if block=="MOGALTUR" & district=="WEST GODAVARI" & state=="ANDHRA PRADESH"
replace block = "BORDUMSA" if block=="BORDUMUSA" & district=="CHANGLANG" & state=="ARUNACHAL PRADESH"
replace block = "DIYUN" if block=="DIYUM" & district=="CHANGLANG" & state=="ARUNACHAL PRADESH"
replace block = "KHIMIYONG" if block=="KHIMIYANG" & district=="CHANGLANG" & state=="ARUNACHAL PRADESH"
replace block = "LAJU" if block=="LAZU" & district=="TIRAP" & state=="ARUNACHAL PRADESH"
replace block = "MECHUKA" if block=="MECHUKHA" & district=="WEST SIANG" & state=="ARUNACHAL PRADESH"
replace block = "KATIGORA" if block=="KATIGORAH" & district=="CACHAR" & state=="ASSAM"
replace block = "KHOIRABARI" if block=="KHIRABARI" & district=="DARRANG" & state=="ASSAM"
replace block = "KATLICHARA" if block=="KATLICHERRA" & district=="HAILAKANDI" & state=="ASSAM"
replace block = "TITABOR" if block=="TITABAR" & district=="JORHAT" & state=="ASSAM"
replace block = "PATHARKANDI" if block=="PATHERKAND" & district=="KARIMGANJ" & state=="ASSAM"
replace block = "DIMOW" if block=="DEMOW" & district=="SIBSAGAR" & state=="ASSAM"
replace block = "BISWANATH" if block=="BISHAWNATH" & district=="SONITPUR" & state=="ASSAM"
replace block = "CHARIDUAR" if block=="CHAIDUAR" & district=="SONITPUR" & state=="ASSAM"
replace block = "NA-DUAR" if block=="NADUAR" & district=="SONITPUR" & state=="ASSAM"
replace block = "CHHORAHI" if block=="CHORAHI" & district=="BEGUSARAI" & state=="BIHAR"
replace block = "CHARPOKHARI" if block=="CHARPOKHRI" & district=="BHOJPUR" & state=="BIHAR"
replace block = "KATIYA" if block=="KATAIYA" & district=="GOPALGANJ" & state=="BIHAR"
replace block = "PACH DEURI" if block=="PACHDEORI" & district=="GOPALGANJ" & state=="BIHAR"
replace block = "GOGRI" if block=="GOGARI" & district=="KHAGARIA" & state=="BIHAR"
replace block = "BARIARPUR" if block=="BARIYARPUR" & district=="MUNGER" & state=="BIHAR"
replace block = "GOBINDPUR" if block=="GOVINDPUR" & district=="NAWADA" & state=="BIHAR"
replace block = "LAURIYA" if block=="LAURIA" & district=="PASHCHIM CHAMPARAN" & state=="BIHAR"
replace block = "BANMA ITAHRI" if block=="BANMA ITARHI" & district=="SAHARSA" & state=="BIHAR"
replace block = "GHAT KUSUMBHA" if block=="GHAT KUSMHA" & district=="SHEIKHPURA" & state=="BIHAR"
replace block = "HASANPURA" if block=="HASAN PURA" & district=="SIWAN" & state=="BIHAR"
replace block = "BHOPALPATTANAM" if block=="BHOPALPATNAM" & district=="DANTEWADA" & state=="CHHATTISGARH"
replace block = "NAGRI" if block=="NAGARI" & district=="DHAMTARI" & state=="CHHATTISGARH"
replace block = "BEMETRA" if block=="BEMETARA" & district=="DURG" & state=="CHHATTISGARH"
replace block = "DONDILUHARA" if block=="DONDILOHARA" & district=="DURG" & state=="CHHATTISGARH"
replace block = "JAIJAIPUR" if block=="JAIJEPUR" & district=="JANJGIR - CHAMPA" & state=="CHHATTISGARH"
replace block = "PATHALGAON" if block=="PATTHALGAON" & district=="JASHPUR" & state=="CHHATTISGARH"
replace block = "RAIPUR" if block=="MAINPUR" & district=="RAIPUR" & state=="CHHATTISGARH"
replace block = "NAVSARI" if block=="NAVSAI" & district=="NAVSARI" & state=="GUJARAT"
replace block = "JHANDUTA" if block=="JHANDUTTA" & district=="BILASPUR" & state=="HIMACHAL PRADESH"
replace block = "SALUNI" if block=="SALOONI" & district=="CHAMBA" & state=="HIMACHAL PRADESH"
replace block = "BARSAR" if block=="BAMSAN" & district=="HAMIRPUR" & state=="HIMACHAL PRADESH"
replace block = "POO" if block=="POOH" & district=="KINNAUR" & state=="HIMACHAL PRADESH"
replace block = "NERMAND" if block=="NIRMAND" & district=="KULLU" & state=="HIMACHAL PRADESH"
replace block = "LAHUL" if block=="LAHAUL" & district=="LAHUL & SPITI" & state=="HIMACHAL PRADESH"
replace block = "DHARMPUR" if block=="DHARAMPUR" & district=="MANDI" & state=="HIMACHAL PRADESH"
replace block = "SUNDARNAGAR" if block=="SUNDERNAGAR" & district=="MANDI" & state=="HIMACHAL PRADESH"
replace block = "SHALAI" if block=="SHILLAI" & district=="SIRMAUR" & state=="HIMACHAL PRADESH"
replace block = "CHADURA" if block=="CHADOORA" & district=="BADGAM" & state=="JAMMU & KASHMIR"
replace block = "BANDIPORE" if block=="BANDIPORA" & district=="BARAMULA" & state=="JAMMU & KASHMIR"
replace block = "BARAMULA" if block=="BARAMULLA" & district=="BARAMULA" & state=="JAMMU & KASHMIR"
replace block = "BISHNA" if block=="BISHNAH" & district=="JAMMU" & state=="JAMMU & KASHMIR"
replace block = "BASHOHLI" if block=="BASOHLI" & district=="KATHUA" & state=="JAMMU & KASHMIR"
replace block = "SHUPIYAN" if block=="SHOPIAN" & district=="PULWAMA" & state=="JAMMU & KASHMIR"
replace block = "NOWSHEHRA" if block=="NOWSHERA" & district=="RAJAURI" & state=="JAMMU & KASHMIR"
replace block = "RAJAURI" if block=="RAJOURI" & district=="RAJAURI" & state=="JAMMU & KASHMIR"
replace block = "GUMIA" if block=="GOMIA" & district=="BOKARO" & state=="JHARKHAND"
replace block = "PETERUAR" if block=="PETERWAR" & district=="BOKARO" & state=="JHARKHAND"
replace block = "GIDHAUR" if block=="GIRDHOUR" & district=="CHATRA" & state=="JHARKHAND"
replace block = "LAWALAUNG" if block=="LAWALONG" & district=="CHATRA" & state=="JHARKHAND"
replace block = "KARON" if block=="KAROWN" & district=="DEOGHAR" & state=="JHARKHAND"
replace block = "DHANBAD-CUM-KENDUADIH-CUM-JAGTA" if block=="DHANBAD-CUM-KENDUADIH-CUM-JOGTA" & district=="DHANBAD" & state=="JHARKHAND"
replace block = "RANISHWAR" if block=="RANESHWER" & district=="DUMKA" & state=="JHARKHAND"
replace block = "SUNDARPAHARI" if block=="SUNDAR PAHARI" & district=="GODDA" & state=="JHARKHAND"
replace block = "KURU" if block=="KURRU" & district=="LOHARDAGA" & state=="JHARKHAND"
replace block = "MAJHGAON" if block=="MANJHGAON" & district=="PASHCHIMI SINGHBHUM" & state=="JHARKHAND"
replace block = "BAHARAGORA" if block=="BAHRAGORA" & district=="PURBI SINGHBHUM" & state=="JHARKHAND"
replace block = "GOLMURI-CUM-JUGSALAI" if block=="GOLMURI CUM JUGSALAI" & district=="PURBI SINGHBHUM" & state=="JHARKHAND"
replace block = "UDHWA" if block=="UDHUA" & district=="SAHIBGANJ" & state=="JHARKHAND"
replace block = "BILGI" if block=="BILAGI" & district=="BAGALKOT" & state=="KARNATAKA"
replace block = "JAMKHANDI" if block=="JAMAKHANDI" & district=="BAGALKOT" & state=="KARNATAKA"
replace block = "DEVANAHALLI" if block=="DEVANHALLI" & district=="BANGALORE RURAL" & state=="KARNATAKA"
replace block = "DOD BALLAPUR" if block=="DODBALLAPUR" & district=="BANGALORE RURAL" & state=="KARNATAKA"
replace block = "HOSAKOTE" if block=="HOSKOTE" & district=="BANGALORE RURAL" & state=="KARNATAKA"
replace block = "HOMNABAD" if block=="HUMNABAD" & district=="BIDAR" & state=="KARNATAKA"
replace block = "KOLLEGAL" if block=="KOLLEGALA" & district=="CHAMARAJANAGAR" & state=="KARNATAKA"
replace block = "CHIKMAGALUR" if block=="CHICKMAGALUR" & district=="CHIKMAGALUR" & state=="KARNATAKA"
replace block = "BANTVAL" if block=="BANTWAL" & district=="DAKSHINA KANNADA" & state=="KARNATAKA"
replace block = "SULYA" if block=="SULLIA" & district=="DAKSHINA KANNADA" & state=="KARNATAKA"
replace block = "HARAPANAHALLI" if block=="HARAPPANAHALLI" & district=="DAVANAGERE" & state=="KARNATAKA"
replace block = "HARIHAR" if block=="HARIHARA" & district=="DAVANAGERE" & state=="KARNATAKA"
replace block = "MUNDARGI" if block=="MUNDARAGI" & district=="GADAG" & state=="KARNATAKA"
replace block = "NARGUND" if block=="NARAGUND" & district=="GADAG" & state=="KARNATAKA"
replace block = "SHIRHATTI" if block=="SHIRAHATTI" & district=="GADAG" & state=="KARNATAKA"
replace block = "CHITAPUR" if block=="CHITTAPUR" & district=="GULBARGA" & state=="KARNATAKA"
replace block = "JEVARGI" if block=="JEWARGI" & district=="GULBARGA" & state=="KARNATAKA"
replace block = "SHAHPUR" if block=="SHAHAPUR" & district=="GULBARGA" & state=="KARNATAKA"
replace block = "HOLE NARSIPUR" if block=="HOLENARSIPUR" & district=="HASSAN" & state=="KARNATAKA"
replace block = "SAKLESHPUR" if block=="SAKALESHPUR" & district=="HASSAN" & state=="KARNATAKA"
replace block = "HANGAL" if block=="HANAGAL" & district=="HAVERI" & state=="KARNATAKA"
replace block = "RANIBENNUR" if block=="RANEBENNUR" & district=="HAVERI" & state=="KARNATAKA"
replace block = "CHIK BALLAPUR" if block=="CHIKBALLAPUR" & district=="KOLAR" & state=="KARNATAKA"
replace block = "SRINIVASPUR" if block=="SRINIVASAPURA" & district=="KOLAR" & state=="KARNATAKA"
replace block = "GANGAWATI" if block=="GANGAVATHI" & district=="KOPPAL" & state=="KARNATAKA"
replace block = "YELBARGA" if block=="YELBURGA" & district=="KOPPAL" & state=="KARNATAKA"
replace block = "MALAVALLI" if block=="MALVALLI" & district=="MANDYA" & state=="KARNATAKA"
replace block = "TIRUMAKUDAL NARSIPUR" if block=="TIRUMAKUDAL-NARSIPUR" & district=="MYSORE" & state=="KARNATAKA"
replace block = "AMBALAPPUZHA" if block=="AMBALAPUZHA" & district=="ALAPPUZHA" & state=="KERALA"
replace block = "CHENGANNUR" if block=="CHENGANNOOR" & district=="ALAPPUZHA" & state=="KERALA"
replace block = "PARAVUR" if block=="PARAVOOR" & district=="ERNAKULAM" & state=="KERALA"
replace block = "TALIPARAMBA" if block=="THALIPARAMBU" & district=="KANNUR" & state=="KERALA"
replace block = "NILAMBUR" if block=="NILAMBOOR" & district=="MALAPPURAM" & state=="KERALA"
replace block = "CHITTUR" if block=="CHITTOOR" & district=="PALAKKAD" & state=="KERALA"
replace block = "MANNARKAD" if block=="MANNARKKAD" & district=="PALAKKAD" & state=="KERALA"
replace block = "OTTAPPALAM" if block=="OTTAPALAM" & district=="PALAKKAD" & state=="KERALA"
replace block = "KODUNGALLUR" if block=="KODUNGALLOOR" & district=="THRISSUR" & state=="KERALA"
replace block = "SULTHANBATHERY" if block=="SULTHANBETHERI" & district=="WAYANAD" & state=="KERALA"
replace block = "THIKRI" if block=="THIKARI" & district=="BARWANI" & state=="MADHYA PRADESH"
replace block = "BERASIA" if block=="BERASIA1" & district=="BHOPAL" & state=="MADHYA PRADESH"
replace block = "NOWGONG" if block=="NOWGAON" & district=="CHHATARPUR" & state=="MADHYA PRADESH"
replace block = "PARASIA" if block=="PARASIYA" & district=="CHHINDWARA" & state=="MADHYA PRADESH"
replace block = "TAMIA" if block=="TAMIYA" & district=="CHHINDWARA" & state=="MADHYA PRADESH"
replace block = "TONK KHURD" if block=="TONKKHURD" & district=="DEWAS" & state=="MADHYA PRADESH"
replace block = "JAWAD" if block=="JAVAD" & district=="NEEMUCH" & state=="MADHYA PRADESH"
replace block = "RAMPUR-BAGHELAN" if block=="RAMPUR BAGHELAN" & district=="SATNA" & state=="MADHYA PRADESH"
replace block = "PUSHPARAJGARH" if block=="PUSHPRAJGARH" & district=="SHAHDOL" & state=="MADHYA PRADESH"
replace block = "ANJANGAON SURJI" if block=="ANJANGAON SUR" & district=="AMRAVATI" & state=="MAHARASHTRA"
replace block = "CHANDUR RAILWAY" if block=="CHANDUR RAILW" & district=="AMRAVATI" & state=="MAHARASHTRA"
replace block = "ARJUNI MORGAON" if block=="ARJUNI MORGAO" & district=="GONDIYA" & state=="MAHARASHTRA"
replace block = "AUNDHA (NAGNATH)" if block=="AUNDHA (NAGNA)" & district=="HINGOLI" & state=="MAHARASHTRA"
replace block = "SHIRUR-ANANTPAL" if block=="SHIRUR-ANANTP" & district=="LATUR" & state=="MAHARASHTRA"
replace block = "KUHOBOTO" if block=="KUHUBOTO" & district=="DIMAPUR" & state=="NAGALAND"
replace block = "CHANGTONGYA" if block=="CHANTONGYA" & district=="MOKOKCHUNG" & state=="NAGALAND"
replace block = "GHATHASHI" if block=="GATHASHI" & district=="ZUNHEBOTO" & state=="NAGALAND"
replace block = "SURUHUTO" if block=="SURUHOTO" & district=="ZUNHEBOTO" & state=="NAGALAND"
replace block = "ATHMALLIK" if block=="ATHAMALLIK" & district=="ANUGUL" & state=="ORISSA"
replace block = "KANIHA" if block=="KARIHA" & district=="ANUGUL" & state=="ORISSA"
replace block = "KISHORENAGAR" if block=="KISHORE NAGAR" & district=="ANUGUL" & state=="ORISSA"
replace block = "BANGOMUNDA" if block=="BANGHAMUNDA" & district=="BALANGIR" & state=="ORISSA"
replace block = "BELPARA" if block=="BELPADA" & district=="BALANGIR" & state=="ORISSA"
replace block = "KHAPRAKHOL" if block=="KHAPARAKHOL" & district=="BALANGIR" & state=="ORISSA"
replace block = "SAINTALA" if block=="SAITALA" & district=="BALANGIR" & state=="ORISSA"
replace block = "TITLAGARH" if block=="TITILAGARH" & district=="BALANGIR" & state=="ORISSA"
replace block = "TUREKELA" if block=="TURIKELA" & district=="BALANGIR" & state=="ORISSA"
replace block = "BALESHWAR SADAR" if block=="BALESWAR SADAR" & district=="BALESHWAR" & state=="ORISSA"
replace block = "BALIAPAL" if block=="BALIPAL" & district=="BALESHWAR" & state=="ORISSA"
replace block = "BHOGRAI" if block=="BHOGORAI" & district=="BALESHWAR" & state=="ORISSA"
replace block = "OUPADA" if block=="OUAPADA" & district=="BALESHWAR" & state=="ORISSA"
replace block = "ATHAGAD" if block=="ATHAGARA" & district=="CUTTACK" & state=="ORISSA"
replace block = "BARANG" if block=="BARANGA" & district=="CUTTACK" & state=="ORISSA"
replace block = "SALEPUR" if block=="SALIPUR" & district=="CUTTACK" & state=="ORISSA"
replace block = "BHUBAN" if block=="BHUABAN" & district=="DHENKANAL" & state=="ORISSA"
replace block = "GANDIA" if block=="GONDIA" & district=="DHENKANAL" & state=="ORISSA"
replace block = "KAMAKSHYANAGAR" if block=="KAMAKHYANAGAR" & district=="DHENKANAL" & state=="ORISSA"
replace block = "PARAJANG" if block=="PARJANG" & district=="DHENKANAL" & state=="ORISSA"
replace block = "KASHINAGARA" if block=="KASHINAGAR" & district=="GAJAPATI" & state=="ORISSA"
replace block = "PARLAKHEMUNDI" if block=="PARALAKHEMUND" & district=="GAJAPATI" & state=="ORISSA"
replace block = "R.UDAYGIRI" if block=="R. UDAYAGIRI" & district=="GAJAPATI" & state=="ORISSA"
replace block = "ASIKA" if block=="ASKA" & district=="GANJAM" & state=="ORISSA"
replace block = "CHHATRAPUR" if block=="CHATRAPUR" & district=="GANJAM" & state=="ORISSA"
replace block = "KHALIKOTE" if block=="KHALLIKOTE" & district=="GANJAM" & state=="ORISSA"
replace block = "PATAPUR" if block=="PATRAPUR" & district=="GANJAM" & state=="ORISSA"
replace block = "PURUSOTTAMPUR" if block=="PURUSHOTTAMPUR" & district=="GANJAM" & state=="ORISSA"
replace block = "DHARAMGARH" if block=="DHARMAGARH" & district=="KALAHANDI" & state=="ORISSA"
replace block = "JAYAPATNA" if block=="JAIPATNA" & district=="KALAHANDI" & state=="ORISSA"
replace block = "KOKASARA" if block=="KOKSARA" & district=="KALAHANDI" & state=="ORISSA"
replace block = "NARALA" if block=="NARLA" & district=="KALAHANDI" & state=="ORISSA"
replace block = "DARINGBADI" if block=="DARINGIBADI" & district=="KANDHAMAL" & state=="ORISSA"
replace block = "TUMUDIBANDHA" if block=="TUMUDIBANDH" & district=="KANDHAMAL" & state=="ORISSA"
replace block = "KENDRAPARA" if block=="KENDRAPADA" & district=="KENDRAPARA" & state=="ORISSA"
replace block = "GHATGAON" if block=="GHATAGAON" & district=="KENDUJHAR" & state=="ORISSA"
replace block = "PATANA" if block=="PATNA" & district=="KENDUJHAR" & state=="ORISSA"
replace block = "BANAPUR" if block=="BANPUR" & district=="KHORDHA" & state=="ORISSA"
replace block = "JEYPUR" if block=="JEYPORE" & district=="KORAPUT" & state=="ORISSA"
replace block = "KUNDURA" if block=="KUNDRA" & district=="KORAPUT" & state=="ORISSA"
replace block = "NARAYANPATANA" if block=="NARAYANPATNA" & district=="KORAPUT" & state=="ORISSA"
replace block = "SIMILIGUDA" if block=="SEMILIGUDA" & district=="KORAPUT" & state=="ORISSA"
replace block = "BANGIRIPOSI" if block=="BANGRIPOSI" & district=="MAYURBHANJ" & state=="ORISSA"
replace block = "BETANATI" if block=="BETNOTI" & district=="MAYURBHANJ" & state=="ORISSA"
replace block = "JASHIPUR" if block=="JASIPUR" & district=="MAYURBHANJ" & state=="ORISSA"
replace block = "RARUAN" if block=="RARUA" & district=="MAYURBHANJ" & state=="ORISSA"
replace block = "RASAGOBINDAPUR" if block=="RASAGOVINDAPUR" & district=="MAYURBHANJ" & state=="ORISSA"
replace block = "TIRINGI" if block=="TIRING" & district=="MAYURBHANJ" & state=="ORISSA"
replace block = "DABUGAN" if block=="DABUGAM" & district=="NABARANGAPUR" & state=="ORISSA"
replace block = "NABARANGAPUR" if block=="NABARANGPUR" & district=="NABARANGAPUR" & state=="ORISSA"
replace block = "PAPARAHANDI" if block=="PAPADAHANDI" & district=="NABARANGAPUR" & state=="ORISSA"
replace block = "UMARKOTE" if block=="UMERKOTE" & district=="NABARANGAPUR" & state=="ORISSA"
replace block = "DASAPALLA" if block=="DASPALLA" & district=="NAYAGARH" & state=="ORISSA"
replace block = "RANAPUR" if block=="RANPUR" & district=="NAYAGARH" & state=="ORISSA"
replace block = "SINAPALI" if block=="SINAPALLI" & district=="NUAPADA" & state=="ORISSA"
replace block = "BRAHMAGIRI" if block=="BRAMHAGIRI" & district=="PURI" & state=="ORISSA"
replace block = "DELANGA" if block=="DELANG" & district=="PURI" & state=="ORISSA"
replace block = "KRUSHNA PRASAD" if block=="KRUSHNAPRASAD" & district=="PURI" & state=="ORISSA"
replace block = "NIMAPADA" if block=="NIMAPARA" & district=="PURI" & state=="ORISSA"
replace block = "KALYANASINGPUR" if block=="KALYANSINGHPUR" & district=="RAYAGADA" & state=="ORISSA"
replace block = "BINIKA" if block=="BINKA" & district=="SONAPUR" & state=="ORISSA"
replace block = "BARAGAON" if block=="BARGAON" & district=="SUNDARGARH" & state=="ORISSA"
replace block = "RAJAGANGAPUR" if block=="RAJGANGPUR" & district=="SUNDARGARH" & state=="ORISSA"
replace block = "TANGARAPALI" if block=="TANGARPALI" & district=="SUNDARGARH" & state=="ORISSA"
replace block = "KHADUR SAHIB" if block=="KHADOOR SAHIB" & district=="AMRITSAR" & state=="PUNJAB"
replace block = "TARN-TARAN" if block=="TARNTARAN" & district=="AMRITSAR" & state=="PUNJAB"
replace block = "BASSI PATHANA" if block=="BASSI PATHANAN" & district=="FATEHGARH SAHIB" & state=="PUNJAB"
replace block = "KHAMANON" if block=="KHAMANO" & district=="FATEHGARH SAHIB" & state=="PUNJAB"
replace block = "FIROZEPUR" if block=="FEROZEPUR" & district=="FIROZPUR" & state=="PUNJAB"
replace block = "GARHSHANKAR" if block=="GARH SHANKAR" & district=="HOSHIARPUR" & state=="PUNJAB"
replace block = "BHAGHA PURANA" if block=="BAGHAPURANA" & district=="MOGA" & state=="PUNJAB"
replace block = "NIHAL SINGHWALA" if block=="NIHAL SINGH WALA" & district=="MOGA" & state=="PUNJAB"
replace block = "GIDDARBAHA" if block=="GIDDERBAHA" & district=="MUKTSAR" & state=="PUNJAB"
replace block = "PEESANGAN" if block=="PISANGAN" & district=="AJMER" & state=="RAJASTHAN"
replace block = "ANTAH" if block=="ANTA" & district=="BARAN" & state=="RAJASTHAN"
replace block = "ANUPGARH" if block=="ANOOPGARH" & district=="GANGANAGAR" & state=="RAJASTHAN"
replace block = "VIRATNAGAR" if block=="VIRATNAGA" & district=="JAIPUR" & state=="RAJASTHAN"
replace block = "MANOHAR THANA" if block=="MANOHARTHANA" & district=="JHALAWAR" & state=="RAJASTHAN"
replace block = "JHUNJHUNUN" if block=="JHUNJHUNU" & district=="JHUNJHUNUN" & state=="RAJASTHAN"
replace block = "NEEM-KA-THANA" if block=="NEEM KA THANA" & district=="SIKAR" & state=="RAJASTHAN"
replace block = "AVANASHI" if block=="AVINASHI" & district=="COIMBATORE" & state=="TAMIL NADU"
replace block = "NILAKKOTTAI" if block=="NILAKOTTAI" & district=="DINDIGUL" & state=="TAMIL NADU"
replace block = "TIRUKALUKUNDRAM" if block=="THIRUKALUKUNDRAM" & district=="KANCHEEPURAM" & state=="TAMIL NADU"
replace block = "KOTAGIRI" if block=="KOTHGIRI" & district=="THE NILGIRIS" & state=="TAMIL NADU"
replace block = "UDHAGAMANDALAM" if block=="UTHAGAMANDALAM" & district=="THE NILGIRIS" & state=="TAMIL NADU"
replace block = "SHENKOTTAI" if block=="SENKOTTAI" & district=="TIRUNELVELI" & state=="TAMIL NADU"
replace block = "ARAKONAM" if block=="ARAKKONAM" & district=="VELLORE" & state=="TAMIL NADU"
replace block = "KALLAKKURICHI" if block=="KALLAKURICHI" & district=="VILUPPURAM" & state=="TAMIL NADU"
replace block = "TIRUKKOYILUR" if block=="TIRUKOILUR" & district=="VILUPPURAM" & state=="TAMIL NADU"
replace block = "CHHAMANU" if block=="CHAWMANU" & district=="DHALAI" & state=="TRIPURA"
replace block = "DAMCHHARA" if block=="DAMCHERRA" & district=="NORTH TRIPURA" & state=="TRIPURA"
replace block = "JAMPUII HILLS" if block=="JAMPUIHILLS" & district=="NORTH TRIPURA" & state=="TRIPURA"
replace block = "PENCHARTHAL" if block=="PECHARTHAL" & district=="NORTH TRIPURA" & state=="TRIPURA"
replace block = "KARBUK" if block=="KARBOOK" & district=="SOUTH TRIPURA" & state=="TRIPURA"
replace block = "MATARBARI" if block=="MATABARI" & district=="SOUTH TRIPURA" & state=="TRIPURA"
replace block = "RUPAICHHARI" if block=="RUPAICHARI" & district=="SOUTH TRIPURA" & state=="TRIPURA"
replace block = "KHAIR" if block=="KAIR" & district=="ALIGARH" & state=="UTTAR PRADESH"
replace block = "HANDIA" if block=="HANDIYA" & district=="ALLAHABAD" & state=="UTTAR PRADESH"
replace block = "AZAMGARH" if block=="AZAMATGARH" & district=="AZAMGARH" & state=="UTTAR PRADESH"
replace block = "BAGHPAT" if block=="BAGPAT" & district=="BAGHPAT" & state=="UTTAR PRADESH"
replace block = "KHEKADA" if block=="KHEKARA" & district=="BAGHPAT" & state=="UTTAR PRADESH"
replace block = "BANDA" if block=="BISANDA" & district=="BANDA" & state=="UTTAR PRADESH"
replace block = "MEERGANJ" if block=="MIRGANJ" & district=="BAREILLY" & state=="UTTAR PRADESH"
replace block = "ANUPSHAHR" if block=="ANNUPSHAHR" & district=="BULANDSHAHAR" & state=="UTTAR PRADESH"
replace block = "DEBAI" if block=="DIBAI" & district=="BULANDSHAHAR" & state=="UTTAR PRADESH"
replace block = "SIANA" if block=="SYANA" & district=="BULANDSHAHAR" & state=="UTTAR PRADESH"
replace block = "CHAKARNAGAR" if block=="CHAKKARNAGAR" & district=="ETAWAH" & state=="UTTAR PRADESH"
replace block = "DADRI" if block=="DADARI" & district=="GAUTAM BUDDHA NAGAR" & state=="UTTAR PRADESH"
replace block = "BILGRAM" if block=="BILAGRAM" & district=="HARDOI" & state=="UTTAR PRADESH"
replace block = "SANDILA" if block=="SANDI" & district=="HARDOI" & state=="UTTAR PRADESH"
replace block = "MARIAHU" if block=="MARIYAHUN" & district=="JAUNPUR" & state=="UTTAR PRADESH"
replace block = "CHAIL" if block=="CHAIAL" & district=="KAUSHAMBI" & state=="UTTAR PRADESH"
replace block = "MOHAMMDI" if block=="MOHAMMADI" & district=="KHERI" & state=="UTTAR PRADESH"
replace block = "MISRIKH" if block=="MISHRIKH" & district=="SITAPUR" & state=="UTTAR PRADESH"
replace block = "DUDHI" if block=="DUDDI" & district=="SONBHADRA" & state=="UTTAR PRADESH"
replace block = "BIGHAPUR" if block=="BIGHA PUR" & district=="UNNAO" & state=="UTTAR PRADESH"
replace block = "BHIKIA SAIN" if block=="BHIKIASAIN" & district=="ALMORA" & state=="UTTARAKHAND"
replace block = "KAPKOT" if block=="KAPKOTE" & district=="BAGESHWAR" & state=="UTTARAKHAND"
replace block = "GAIR SAIN" if block=="GARSAIN" & district=="CHAMOLI" & state=="UTTARAKHAND"
replace block = "THALI SAIN" if block=="THALISAIN" & district=="GARHWAL" & state=="UTTARAKHAND"
replace block = "MUNSIARI" if block=="MUNSYARI" & district=="PITHORAGARH" & state=="UTTARAKHAND"
replace block = "DEVPRAYAG" if block=="DEVPARYAG" & district=="TEHRI GARHWAL" & state=="UTTARAKHAND"
replace block = "NARENDRANAGAR" if block=="NARENDRA NAGAR" & district=="TEHRI GARHWAL" & state=="UTTARAKHAND"
replace block = "PRATAPNAGAR" if block=="PARTAPNAGAR" & district=="TEHRI GARHWAL" & state=="UTTARAKHAND"
replace block = "PURAULA" if block=="PUROLA" & district=="UTTARKASHI" & state=="UTTARAKHAND"
replace block = "GANGAJALGHATI" if block=="GANGAJAL GHATI" & district=="BANKURA" & state=="WEST BENGAL"
replace block = "JAMALPUR" if block=="JAMAL PUR" & district=="BARDDHAMAN" & state=="WEST BENGAL"
replace block = "PANDABESWAR" if block=="PANDAVESWAR" & district=="BARDDHAMAN" & state=="WEST BENGAL"
replace block = "BOLPUR SRINIKETAN" if block=="BOLPUR-SRINIKETAN" & district=="BIRBHUM" & state=="WEST BENGAL"
replace block = "HILLI" if block=="HILI" & district=="DAKSHIN DINAJPUR" & state=="WEST BENGAL"
replace block = "KUSHMUNDI" if block=="KUSHMANDI" & district=="DAKSHIN DINAJPUR" & state=="WEST BENGAL"
replace block = "DARJEELING PULBAZAR" if block=="DARJEELING-PULBAZAR" & district=="DARJILING" & state=="WEST BENGAL"
replace block = "JOREBUNGLOW SUKIAPOKHRI" if block=="JORE BUNGLOW-SUKIAPOKHRI" & district=="DARJILING" & state=="WEST BENGAL"
replace block = "NAXALBARI" if block=="NAXAL BARI" & district=="DARJILING" & state=="WEST BENGAL"
replace block = "BALLY JAGACHHA" if block=="BALLY-JAGACHA" & district=="HAORA" & state=="WEST BENGAL"
replace block = "BAMANGOLA" if block=="BAMONGOLA" & district=="MALDAH" & state=="WEST BENGAL"
replace block = "MALDAH (OLD)" if block=="MALDA (OLD)" & district=="MALDAH" & state=="WEST BENGAL"
replace block = "JAMBONI" if block=="JAMBANI" & district=="MEDINIPUR" & state=="WEST BENGAL"
replace block = "NANDA KUMAR" if block=="NANDAKUMAR" & district=="MEDINIPUR" & state=="WEST BENGAL"
replace block = "MURSHIDABAD JIAGANJ" if block=="MURSHIDABAD-JIAGUNJ" & district=="MURSHIDABAD" & state=="WEST BENGAL"
replace block = "SAMSERGANJ" if block=="SHAMSHERGANJ" & district=="MURSHIDABAD" & state=="WEST BENGAL"
replace block = "PATHARPRATIMA" if block=="PATHAR PRATIMA" & district=="SOUTH TWENTY FOUR PARGANAS" & state=="WEST BENGAL"
replace block = "SONARPUR" if block=="SONAR PUR" & district=="SOUTH TWENTY FOUR PARGANAS" & state=="WEST BENGAL"
replace block = "NERADIGONDA" if block=="NEERADIGONDA" & district=="ADILABAD" & state=="ANDHRA PRADESH"
replace block = "PICHATUR" if block=="PITCHATUR" & district=="CHITTOOR" & state=="ANDHRA PRADESH"
replace block = "BALANGIR" if block=="BOLANGIR" & district=="BALANGIR" & state=="ORISSA"
replace block = "VISHNUPUR" if block=="BISHNUPUR" & district=="BANKURA" & state=="WEST BENGAL"
replace block = "SANTHIPURAM" if block=="SANTHIPURAM H/O ARINUTHALAPALLE" & district=="CHITTOOR" & state=="ANDHRA PRADESH"
replace block = "SAVALYAPURAM H/O KANAMARLAPUDI" if block=="SAVALYAPURAM KANUMARLAPUDI" & district=="GUNTUR" & state=="ANDHRA PRADESH"
replace block = "MALHARRAO" if block=="MALHARRAO MANDAL" & district=="KARIMNAGAR" & state=="ANDHRA PRADESH"
replace block = "MUTHARAM (MAHADEVPUR)" if block=="MUTHARAM (A)" & district=="KARIMNAGAR" & state=="ANDHRA PRADESH"
replace block = "BUCHIREDDIPALEM" if block=="BUCHIREDDIPALEM H/O VAVVERU" & district=="NELLORE" & state=="ANDHRA PRADESH"
replace block = "GOLAGHAT" if block=="GOLAGHAT CENTRAL" & district=="GOLAGHAT" & state=="ASSAM"
replace block = "DHAKUAKHANA (PART I)" if block=="DHAKUAKHANA" & district=="LAKHIMPUR" & state=="ASSAM"
replace block = "GAYA TOWN" if block=="GAYA" & district=="GAYA" & state=="BIHAR"
replace block = "SONBHADRA BANSHI SURYAPUR" if block=="SONBHADRA BANSHI" & district=="JEHANABAD" & state=="BIHAR"
replace block = "PATNA RURAL" if block=="PATNA RURAL (A) PATNA RURAL (B)" & district=="PATNA" & state=="BIHAR"
replace block = "AMBAGARH" if block=="AMBAGARH CHOWKI" & district=="RAJNANDGAON" & state=="CHHATTISGARH"
replace block = "DADRI" if block=="DADRI I (PART)" & district=="BHIWANI" & state=="HARYANA"
replace block = "GHARAUNDA" if block=="GHARAUNDA (PART)" & district=="KARNAL" & state=="HARYANA"
replace block = "JUBBAL" if block=="JUBBAL KOTKHAI" & district=="SHIMLA" & state=="HIMACHAL PRADESH"
replace block = "BHALESSA (GANDOH)" if block=="BHALESSA" & district=="DODA" & state=="JAMMU & KASHMIR"
replace block = "GOOL GULAB GARH" if block=="GOOL" & district=="UDHAMPUR" & state=="JAMMU & KASHMIR"
replace block = "THIRUVANANTHAPURAM" if block=="THIRUVANANTHAPURAM RURAL" & district=="THIRUVANANTHAPURAM" & state=="KERALA"
replace block = "DHAMANGAON RAILWAY" if block=="DHAMANGAON RA" & district=="AMRAVATI" & state=="MAHARASHTRA"
replace block = "DESAIGANJ (VADASA)" if block=="DESAIGANJ (VA)" & district=="GADCHIROLI" & state=="MAHARASHTRA"
replace block = "EDLABAD (MUKTAINAGAR)" if block=="EDLABAD (MUKT)" & district=="JALGAON" & state=="MAHARASHTRA"
replace block = "NAIGAON (KHAIRGAON)" if block=="NAIGAON (KHAI)" & district=="NANDED" & state=="MAHARASHTRA"
replace block = "THINGSULTHLIAH (PART)" if block=="THINGSULTHLIAH" & district=="AIZAWL" & state=="MIZORAM"
replace block = "TLANGNUAM (PART)" if block=="TLANGNUAM" & district=="AIZAWL" & state=="MIZORAM"
replace block = "EAST LUNGDAR (PART)" if block=="EAST LUNGDAR" & district=="SERCHHIP" & state=="MIZORAM"
replace block = "KOHIMA SADAR" if block=="KOHIMA" & district=="KOHIMA" & state=="NAGALAND"
replace block = "MON SADAR" if block=="MON" & district=="MON" & state=="NAGALAND"
replace block = "PHEK SADAR" if block=="PHEK" & district=="PHEK" & state=="NAGALAND"
replace block = "KIPHIRE SADAR" if block=="KIPHIRE" & district=="TUENSANG" & state=="NAGALAND"
replace block = "WOKHA SADAR" if block=="WOKHA" & district=="WOKHA" & state=="NAGALAND"
replace block = "ZUNHEBOTO SADAR" if block=="ZUNHEBOTO" & district=="ZUNHEBOTO" & state=="NAGALAND"
replace block = "TALCHER SADAR" if block=="TALCHER" & district=="ANUGUL" & state=="ORISSA"
replace block = "BANKI" if block=="BANKI DOMOPARA" & district=="CUTTACK" & state=="ORISSA"
replace block = "TANGI" if block=="TANGI AND CHOUDWAR" & district=="CUTTACK" & state=="ORISSA"
replace block = "BARIPADA SADAR" if block=="BARIPADA" & district=="MAYURBHANJ" & state=="ORISSA"
replace block = "RAMPURA PHUL" if block=="RAMPURA" & district=="BATHINDA" & state=="PUNJAB"
replace block = "DUDU (HQ. MAUZAMABAD)" if block=="DUDU" & district=="JAIPUR" & state=="RAJASTHAN"
replace block = "JAMWA RAMGARH" if block=="JAMWA RAM" & district=="JAIPUR" & state=="RAJASTHAN"
replace block = "MARWAR JUNCTION" if block=="MARWAR JU" & district=="PALI" & state=="RAJASTHAN"
replace block = "MAWANA" if block=="MAWANA KALA" & district=="MEERUT" & state=="UTTAR PRADESH"
replace block = "MATHABHANGA II" if block=="MATHA BHANGA II" & district=="KOCH BIHAR" & state=="WEST BENGAL"
replace block = "HARISCHANDRAPUR I" if block=="HARISHCHANDRAPUR I" & district=="MALDAH" & state=="WEST BENGAL"
replace block = "HARISCHANDRAPUR II" if block=="HARISHCHANDRAPUR II" & district=="MALDAH" & state=="WEST BENGAL"
replace block = "BHAGAWANPUR I" if block=="BHAGABANPUR I" & district=="MEDINIPUR" & state=="WEST BENGAL"
replace block = "BHAGAWANPUR II" if block=="BHAGABANPUR II" & district=="MEDINIPUR" & state=="WEST BENGAL"
replace block = "GOPIBALLAVPUR II" if block=="GOPIBALLAV PUR II" & district=="MEDINIPUR" & state=="WEST BENGAL"
replace block = "POTASHPUR I" if block=="PATASHPUR I" & district=="MEDINIPUR" & state=="WEST BENGAL"
replace block = "POTASHPUR II" if block=="PATASHPUR II" & district=="MEDINIPUR" & state=="WEST BENGAL"
replace block = "BHAGAWANGOLA I" if block=="BHAGOBANGOLA I" & district=="MURSHIDABAD" & state=="WEST BENGAL"
replace block = "RAGHUNATHPUR I" if block=="RAGHUNATH PUR I" & district=="PURULIYA" & state=="WEST BENGAL"
replace block = "CHINNAMANDEM" if block=="CHINNAMUDIAM" & district=="CUDDAPAH" & state=="ANDHRA PRADESH"
replace block = "BISHAMAKATAK" if block=="BISSAMCUTTACK" & district=="RAYAGADA" & state=="ORISSA"
replace block = "SERAIKELA" if block=="SERAIKELLA" & district=="PASHCHIMI SINGHBHUM" & state=="JHARKHAND"
replace block = "HARSUD" if block=="HARSOOD" & district=="EAST NIMAR" & state=="MADHYA PRADESH"
replace block = "KHANSAHIB" if block=="KHAN-SAHIB" & district=="BADGAM" & state=="JAMMU & KASHMIR"
replace block = "KHALSI" if block=="KHALTSI" & district=="LEH (LADAKH)" & state=="JAMMU & KASHMIR"
replace block = "SANKU" if block=="SANKOO" & district=="KARGIL" & state=="JAMMU & KASHMIR"
replace block = "ANI" if block=="ANNI" & district=="KULLU" & state=="HIMACHAL PRADESH"
replace block = "CHINIYALISAUR" if block=="CHINYALISAUR" & district=="UTTARKASHI" & state=="UTTARAKHAND"
replace block = "JAKHNIDHAR" if block=="JAKHANIDHAR" & district=="TEHRI GARHWAL" & state=="UTTARAKHAND"
replace block = "GARUD" if block=="GARUR" & district=="BAGESHWAR" & state=="UTTARAKHAND"
replace block = "CHAUKHUTIYA" if block=="CHAUKHUTIA" & district=="ALMORA" & state=="UTTARAKHAND"
replace block = "BAJPUR" if block=="BAZPUR" & district=="UDHAM SINGH NAGAR" & state=="UTTARAKHAND"
replace block = "MATENHAIL" if block=="MATANHAIL" & district=="JHAJJAR" & state=="HARYANA"
replace block = "MUNGIAKUMI" if block=="MUNGIAKAMI" & district=="WEST TRIPURA" & state=="TRIPURA"
replace block = "MORANGI" if block=="MORONGI" & district=="GOLAGHAT" & state=="ASSAM"
replace block = "DERABISH" if block=="DERABIS" & district=="KENDRAPARA" & state=="ORISSA"
replace block = "ASTARANGA" if block=="ASTARANG" & district=="PURI" & state=="ORISSA"
replace block = "JAGANNATH PRASAD" if block=="JAGANNATHPRASAD" & district=="GANJAM" & state=="ORISSA"
replace block = "POLASARA" if block=="POLOSARA" & district=="GANJAM" & state=="ORISSA"
replace block = "SERAGAD" if block=="SHERAGADA" & district=="GANJAM" & state=="ORISSA"
replace block = "BATOULI" if block=="BATAULI" & district=="SURGUJA" & state=="CHHATTISGARH"
replace block = "OUDGI" if block=="ODGI" & district=="SURGUJA" & state=="CHHATTISGARH"
replace block = "UDAYPUR" if block=="UDAIPUR" & district=="SURGUJA" & state=="CHHATTISGARH"
replace block = "FARSABAHAR" if block=="PHARSABAHAR" & district=="JASHPUR" & state=="CHHATTISGARH"
replace block = "PATHARIYA" if block=="PATHARIA" & district=="BILASPUR" & state=="CHHATTISGARH"
replace block = "BAGBAHRA" if block=="BAGBAHARA" & district=="MAHASAMUND" & state=="CHHATTISGARH"
replace block = "DURGKONDAL" if block=="DURGUCONDAL" & district=="KANKER" & state=="CHHATTISGARH"
replace block = "BADE RAJPUR" if block=="BADERAJPUR" & district=="BASTAR" & state=="CHHATTISGARH"
replace block = "BAKAVAND" if block=="BAKABAND" & district=="BASTAR" & state=="CHHATTISGARH"
replace block = "GIDAM" if block=="GEEDAM" & district=="DANTEWADA" & state=="CHHATTISGARH"
replace block = "KUAKONDA" if block=="KUWAKONDA" & district=="DANTEWADA" & state=="CHHATTISGARH"
replace block = "MALTHON" if block=="MALTHONE" & district=="SAGAR" & state=="MADHYA PRADESH"
replace block = "ATHNER" if block=="ATHNAIR" & district=="BETUL" & state=="MADHYA PRADESH"
replace block = "GHODA DONGRI" if block=="GHORADONGRI" & district=="BETUL" & state=="MADHYA PRADESH"
replace block = "MAJHOLI" if block=="MAJHAULI" & district=="JABALPUR" & state=="MADHYA PRADESH"
replace block = "GHUGHARI" if block=="GHUGHRI" & district=="MANDLA" & state=="MADHYA PRADESH"
replace block = "MOHKHED" if block=="MOHKHEDA" & district=="CHHINDWARA" & state=="MADHYA PRADESH"
replace block = "ELGAID" if block=="ELEGAID" & district=="KARIMNAGAR" & state=="ANDHRA PRADESH"
replace block = "THANDRAMPET" if block=="THANDARAMPET" & district=="TIRUVANNAMALAI" & state=="TAMIL NADU"
replace block = "GUNNOR" if block=="GUNOUR" & district=="PANNA" & state=="MADHYA PRADESH"
replace block = "BHUBANESWAR (M.CORP.)" if block=="BHUBANESWAR" & district=="KHORDHA" & state=="ORISSA"
replace block = "JAMAI" if block=="JAMAI (JUNNARDEO)" & district=="CHHINDWARA" & state=="MADHYA PRADESH"
replace block = "RAJWARA" if block=="RAJWAR" & district=="KUPWARA" & state=="JAMMU & KASHMIR"
replace block = "TANGDHAR" if block=="TANGDAR" & district=="KUPWARA" & state=="JAMMU & KASHMIR"
replace block = "TEETWAL" if block=="TEETHWAL" & district=="KUPWARA" & state=="JAMMU & KASHMIR"
replace block = "RUHAMA" if block=="ROHAMA" & district=="BARAMULA" & state=="JAMMU & KASHMIR"
replace block = "WAGURA" if block=="WAGOORA" & district=="BARAMULA" & state=="JAMMU & KASHMIR"
replace block = "KAKA PORA" if block=="KAKAPORA" & district=="PULWAMA" & state=="JAMMU & KASHMIR"
replace block = "KOVERI PORA" if block=="KOVIRIPORA" & district=="ANANTNAG" & state=="JAMMU & KASHMIR"
replace block = "QUIMOH" if block=="QAIMOH" & district=="ANANTNAG" & state=="JAMMU & KASHMIR"
replace block = "KHAROO" if block=="KHARU" & district=="LEH (LADAKH)" & state=="JAMMU & KASHMIR"
replace block = "SHAKAR CHIKTAN" if block=="SHAKAR-CHIKTAN" & district=="KARGIL" & state=="JAMMU & KASHMIR"
replace block = "SHARGOL" if block=="SHARGOLE" & district=="KARGIL" & state=="JAMMU & KASHMIR"
replace block = "BHAGWA" if block=="BHAGWAH" & district=="DODA" & state=="JAMMU & KASHMIR"
replace block = "RAMSU" if block=="RAMSOO" & district=="DODA" & state=="JAMMU & KASHMIR"
replace block = "PANCHARI" if block=="PANCHRAI" & district=="UDHAMPUR" & state=="JAMMU & KASHMIR"
replace block = "R.S. PURA" if block=="R.S.PURA" & district=="JAMMU" & state=="JAMMU & KASHMIR"
replace block = "LOHAI MALHAR" if block=="LOHAI-MALHAR" & district=="KATHUA" & state=="JAMMU & KASHMIR"
replace block = "DERA" if block=="DEHRA" & district=="KANGRA" & state=="HIMACHAL PRADESH"
replace block = "LAMBAGRAON" if block=="LAMBAGAON" & district=="KANGRA" & state=="HIMACHAL PRADESH"
replace block = "NAGAR" if block=="NAGGAR" & district=="KULLU" & state=="HIMACHAL PRADESH"
replace block = "BIJHRI" if block=="BIJHARI" & district=="HAMIRPUR" & state=="HIMACHAL PRADESH"
replace block = "DHARMPUR" if block=="DHARAMPUR" & district=="SOLAN" & state=="HIMACHAL PRADESH"
replace block = "CHAUHRA" if block=="CHAUHARA" & district=="SHIMLA" & state=="HIMACHAL PRADESH"
replace block = "CHAUPAL" if block=="CHOPAL" & district=="SHIMLA" & state=="HIMACHAL PRADESH"
replace block = "DINANAGAR" if block=="DINA NAGAR" & district=="GURDASPUR" & state=="PUNJAB"
replace block = "FATEHGARH CHURIAN" if block=="FATEH GARH CHURIAN" & district=="GURDASPUR" & state=="PUNJAB"
replace block = "CHOLA SAHIB" if block=="CHOHLA SAHIB" & district=="AMRITSAR" & state=="PUNJAB"
replace block = "TARSIKA" if block=="TARSIKKA" & district=="AMRITSAR" & state=="PUNJAB"
replace block = "HAZIPUR" if block=="HAJIPUR" & district=="HOSHIARPUR" & state=="PUNJAB"
replace block = "DHARMKOT" if block=="DHARAMKOT" & district=="MOGA" & state=="PUNJAB"
replace block = "GURU HAR SAHAI" if block=="GURUHAR SAHAI" & district=="FIROZPUR" & state=="PUNJAB"
replace block = "KOT KAPURA" if block=="KOTKAPURA" & district=="FARIDKOT" & state=="PUNJAB"
replace block = "BHUNERHERI" if block=="BHUNNERHERI" & district=="PATIALA" & state=="PUNJAB"
replace block = "GHANAUR" if block=="GHANOUR" & district=="PATIALA" & state=="PUNJAB"
replace block = "SANAUR" if block=="SANOUR" & district=="PATIALA" & state=="PUNJAB"
replace block = "DASHOLI" if block=="DASOLI" & district=="CHAMOLI" & state=="UTTARAKHAND"
replace block = "DEWAL" if block=="DEVAL" & district=="CHAMOLI" & state=="UTTARAKHAND"
replace block = "GHAT" if block=="GHAAT" & district=="CHAMOLI" & state=="UTTARAKHAND"
replace block = "AUGUST MUNI" if block=="AUGUSTMUNI" & district=="RUDRAPRAYAG" & state=="UTTARAKHAND"
replace block = "BIROKHAL" if block=="BIRONKHAL" & district=="GARHWAL" & state=="UTTARAKHAND"
replace block = "DOGADDA" if block=="DUGADDA" & district=="GARHWAL" & state=="UTTARAKHAND"
replace block = "NAUNIDANDA" if block=="NAINIDANDA" & district=="GARHWAL" & state=="UTTARAKHAND"
replace block = "KANALI CHHINA" if block=="KANALICHHINA" & district=="PITHORAGARH" & state=="UTTARAKHAND"
replace block = "BHAISIYA CHHANA" if block=="BHAISIACHHANA" & district=="ALMORA" & state=="UTTARAKHAND"
replace block = "HAWAL BAGH" if block=="HAWALBAG" & district=="ALMORA" & state=="UTTARAKHAND"
replace block = "LAMGARHA" if block=="LAMGARA" & district=="ALMORA" & state=="UTTARAKHAND"
replace block = "OKHAL KANDA" if block=="OKHALKANDA" & district=="NAINITAL" & state=="UTTARAKHAND"
replace block = "PILLU KHERA" if block=="PILLUKHERA" & district=="JIND" & state=="HARYANA"
replace block = "SAHLAWAS" if block=="SALHAWAS" & district=="JHAJJAR" & state=="HARYANA"
replace block = "NIMRANA" if block=="NEEMRANA" & district=="ALWAR" & state=="RAJASTHAN"
replace block = "UMREN" if block=="UMRAIN" & district=="ALWAR" & state=="RAJASTHAN"
replace block = "GOVINDGARH" if block=="GOVINDGAR" & district=="JAIPUR" & state=="RAJASTHAN"
replace block = "DHOND" if block=="DHOD" & district=="SIKAR" & state=="RAJASTHAN"
replace block = "BARGAON" if block=="BADGAON" & district=="UDAIPUR" & state=="RAJASTHAN"
replace block = "BHOPALSAGAR" if block=="BHUPALSAGAR" & district=="CHITTAURGARH" & state=="RAJASTHAN"
replace block = "DAG" if block=="DUG" & district=="JHALAWAR" & state=="RAJASTHAN"
replace block = "NEHTAUR" if block=="NETAUR" & district=="BIJNOR" & state=="UTTAR PRADESH"
replace block = "ASMAULI" if block=="ASMOLI" & district=="MORADABAD" & state=="UTTAR PRADESH"
replace block = "BANIYAKHERA" if block=="BANIAKHERA" & district=="MORADABAD" & state=="UTTAR PRADESH"
replace block = "BHAGATPUR TANDA" if block=="BHARATPUR TANDA" & district=="MORADABAD" & state=="UTTAR PRADESH"
replace block = "DINGARPUR" if block=="DINGPUR" & district=="MORADABAD" & state=="UTTAR PRADESH"
replace block = "PANWASA" if block=="PAWASA" & district=="MORADABAD" & state=="UTTAR PRADESH"
replace block = "NOHJHIL" if block=="NAUHJHIL" & district=="MATHURA" & state=="UTTAR PRADESH"
replace block = "AMANPUR" if block=="AMAPUR" & district=="ETAH" & state=="UTTAR PRADESH"
replace block = "SIDHPURA" if block=="SIRHPURA" & district=="ETAH" & state=="UTTAR PRADESH"
replace block = "BHUTA" if block=="BHUNTA" & district=="BAREILLY" & state=="UTTAR PRADESH"
replace block = "BITHRICHAINPUR" if block=="BITHIRI CHAINPUR" & district=="BAREILLY" & state=="UTTAR PRADESH"
replace block = "RAMNAGAR" if block=="RAM NAGAR" & district=="BAREILLY" & state=="UTTAR PRADESH"
replace block = "AMARIYA" if block=="AMARIA" & district=="PILIBHIT" & state=="UTTAR PRADESH"
replace block = "BHAWAL KHERA" if block=="BHWALKHERA" & district=="SHAHJAHANPUR" & state=="UTTAR PRADESH"
replace block = "MITAULI" if block=="MITOLI" & district=="KHERI" & state=="UTTAR PRADESH"
replace block = "BEHADAR" if block=="BEHDAR" & district=="HARDOI" & state=="UTTAR PRADESH"
replace block = "HARIYAWAN" if block=="HARIYAWA" & district=="HARDOI" & state=="UTTAR PRADESH"
replace block = "KACHHAUNA" if block=="KACHAUNA" & district=="HARDOI" & state=="UTTAR PRADESH"
replace block = "MADHOGANJ" if block=="MADHOVGANJ" & district=="HARDOI" & state=="UTTAR PRADESH"
replace block = "MALLAWAN" if block=="MALLAWA" & district=="HARDOI" & state=="UTTAR PRADESH"
replace block = "TONDARPUR" if block=="TONDERPUR" & district=="HARDOI" & state=="UTTAR PRADESH"
replace block = "AURAS" if block=="AURAAS" & district=="UNNAO" & state=="UTTAR PRADESH"
replace block = "GANJ MORADABAD" if block=="GANJMORADABAD" & district=="UNNAO" & state=="UTTAR PRADESH"
replace block = "MIANGANJ" if block=="MIAGANJ" & district=="UNNAO" & state=="UTTAR PRADESH"
replace block = "SAROJANINAGAR" if block=="SAROJNI NAGAR" & district=="LUCKNOW" & state=="UTTAR PRADESH"
replace block = "KHIRON" if block=="KHIRO" & district=="RAE BARELI" & state=="UTTAR PRADESH"
replace block = "ROHNIYA" if block=="ROHINIYAN" & district=="RAE BARELI" & state=="UTTAR PRADESH"
replace block = "BARHPURA" if block=="BARAHPUR" & district=="FARRUKHABAD" & state=="UTTAR PRADESH"
replace block = "AJIT MAL" if block=="AJITMAL" & district=="AURAIYA" & state=="UTTAR PRADESH"
replace block = "ERWA KATRA" if block=="EKHA KATRA" & district=="AURAIYA" & state=="UTTAR PRADESH"
replace block = "SARBANKHERA" if block=="SARVANKHEDA" & district=="KANPUR DEHAT" & state=="UTTAR PRADESH"
replace block = "CHAUBEPUR" if block=="CHAUBEYPUR" & district=="KANPUR NAGAR" & state=="UTTAR PRADESH"
replace block = "DAKOR" if block=="DAKORE" & district=="JALAUN" & state=="UTTAR PRADESH"
replace block = "KUTHAUND" if block=="KUTHOND" & district=="JALAUN" & state=="UTTAR PRADESH"
replace block = "MAHEVA" if block=="MAHEWA" & district=="JALAUN" & state=="UTTAR PRADESH"
replace block = "GURSARAI" if block=="GURSRAI" & district=="JHANSI" & state=="UTTAR PRADESH"
replace block = "MAHRAUNI" if block=="MAHRAUNA" & district=="LALITPUR" & state=="UTTAR PRADESH"
replace block = "MUSKARA" if block=="MUSKRA" & district=="HAMIRPUR" & state=="UTTAR PRADESH"
replace block = "JAITPUR" if block=="JYOTPUR" & district=="MAHOBA" & state=="UTTAR PRADESH"
replace block = "TELYANI" if block=="TELIYANI" & district=="FATEHPUR" & state=="UTTAR PRADESH"
replace block = "LAKSHAMANPUR" if block=="LAKSHMANPUR" & district=="PRATAPGARH" & state=="UTTAR PRADESH"
replace block = "SANGIPUR" if block=="SINGHIPUR" & district=="PRATAPGARH" & state=="UTTAR PRADESH"
replace block = "KAURIHAR" if block=="KAUDIHAAR" & district=="ALLAHABAD" & state=="UTTAR PRADESH"
replace block = "MAUAIMA" if block=="MAU AIMA" & district=="ALLAHABAD" & state=="UTTAR PRADESH"
replace block = "DUBEY PUR" if block=="DUBEPUR" & district=="SULTANPUR" & state=="UTTAR PRADESH"
replace block = "TAJWAPUR" if block=="TEJWAPUR" & district=="BAHRAICH" & state=="UTTAR PRADESH"
replace block = "KHESRAHA" if block=="KHESRHA" & district=="SIDDHARTHNAGAR" & state=="UTTAR PRADESH"
replace block = "NATH NAGAR" if block=="NATHNAGAR" & district=="SANT KABIR NAGAR" & state=="UTTAR PRADESH"
replace block = "JANGAL KODIA" if block=="JANGAL KAUDIA" & district=="GORAKHPUR" & state=="UTTAR PRADESH"
replace block = "KAURI RAM" if block=="KAURIRAM" & district=="GORAKHPUR" & state=="UTTAR PRADESH"
replace block = "PATHAR DEWA" if block=="PATHARDEWA" & district=="DEORIA" & state=="UTTAR PRADESH"
replace block = "BILARIYAGANJ" if block=="BILARIAGANJ" & district=="AZAMGARH" & state=="UTTAR PRADESH"
replace block = "MARTINGANJ" if block=="MATINGANJ" & district=="AZAMGARH" & state=="UTTAR PRADESH"
replace block = "PALHNA" if block=="PALHANA" & district=="AZAMGARH" & state=="UTTAR PRADESH"
replace block = "DOHARI GHAT" if block=="DOHRI GHAT" & district=="MAU" & state=="UTTAR PRADESH"
replace block = "DHARAMPUR" if block=="DHARMAPUR" & district=="JAUNPUR" & state=="UTTAR PRADESH"
replace block = "MOGRA BADSHAHPUR" if block=="MUGRA BADSHAHPUR" & district=="JAUNPUR" & state=="UTTAR PRADESH"
replace block = "SUITHAKALA" if block=="SUITHKALA" & district=="JAUNPUR" & state=="UTTAR PRADESH"
replace block = "MAJHAWA" if block=="MAJHAWAN" & district=="MIRZAPUR" & state=="UTTAR PRADESH"
replace block = "SIKHAR" if block=="SIKHDI" & district=="MIRZAPUR" & state=="UTTAR PRADESH"
replace block = "RAMLE BANGO" if block=="RAMLE-BANGO" & district=="EAST SIANG" & state=="ARUNACHAL PRADESH"
replace block = "ETALIN-MALINEY" if block=="ETALIN-MALINYE" & district=="DIBANG VALLEY" & state=="ARUNACHAL PRADESH"
replace block = "WOZHURO RALAN" if block=="WOZURO-RALAN" & district=="WOKHA" & state=="NAGALAND"
replace block = "CHAPAR-SALKOCHA" if block=="CHAPAP-SALKOCHA" & district=="DHUBRI" & state=="ASSAM"
replace block = "KUSHDHAWA" if block=="KUSHDHWA" & district=="GOALPARA" & state=="ASSAM"
replace block = "CHAKCHAKA" if block=="CHAK-CHAKA" & district=="BARPETA" & state=="ASSAM"
replace block = "SUALKUCHI" if block=="SULKUCHI" & district=="KAMRUP" & state=="ASSAM"
replace block = "BHURBANDHA" if block=="BHUR-BANDHA" & district=="MARIGAON" & state=="ASSAM"
replace block = "KAPILI PT." if block=="KALILI PT." & district=="NAGAON" & state=="ASSAM"
replace block = "LAOKHOWA" if block=="LAO-KHOWA" & district=="NAGAON" & state=="ASSAM"
replace block = "KAKODONGA" if block=="LAKODONGA" & district=="GOLAGHAT" & state=="ASSAM"
replace block = "JATING VALLEY" if block=="JATINGA VALLEY" & district=="NORTH CACHAR HILLS" & state=="ASSAM"
replace block = "BARKHOLA" if block=="BAR-KHOLAA" & district=="CACHAR" & state=="ASSAM"
replace block = "RAJABAZAR" if block=="RAJA BAZAR" & district=="CACHAR" & state=="ASSAM"
replace block = "SALCHAPRA" if block=="SAL-CHAPRA" & district=="CACHAR" & state=="ASSAM"
replace block = "DULLAVCHERRA" if block=="DULLAV CHERRA" & district=="KARIMGANJ" & state=="ASSAM"
replace block = "KUANRMUNDA" if block=="KUARMUNDA" & district=="SUNDARGARH" & state=="ORISSA"
replace block = "SHAMAKHUNTA" if block=="SAMAKHUNTA" & district=="MAYURBHANJ" & state=="ORISSA"
replace block = "SIMILIA" if block=="SIMULIA" & district=="BALESHWAR" & state=="ORISSA"
replace block = "DANAGADI" if block=="DANGADI" & district=="JAJAPUR" & state=="ORISSA"
replace block = "JAJAPUR" if block=="JAJPUR" & district=="JAJAPUR" & state=="ORISSA"
replace block = "BANARPAL" if block=="BANRAPAL" & district=="ANUGUL" & state=="ORISSA"
replace block = "PHULABANI" if block=="PHULBANI" & district=="KANDHAMAL" & state=="ORISSA"
replace block = "PUINTALA" if block=="PUITALA" & district=="BALANGIR" & state=="ORISSA"
replace block = "KOLNARA" if block=="KOLANARA" & district=="RAYAGADA" & state=="ORISSA"
replace block = "KHAIRAPUT" if block=="KHAIRPUT" & district=="MALKANGIRI" & state=="ORISSA"
replace block = "GARIABAND" if block=="GARIYABAND" & district=="RAIPUR" & state=="CHHATTISGARH"
replace block = "GANGEV" if block=="GANGEO" & district=="REWA" & state=="MADHYA PRADESH"
replace block = "KATTIWADA" if block=="KATTHIWADA" & district=="JHABUA" & state=="MADHYA PRADESH"
replace block = "BALDI" if block=="BALADI" & district=="EAST NIMAR" & state=="MADHYA PRADESH"
replace block = "KOOTHUPARAMBA" if block=="KOOTHUPARAMBU" & district=="KANNUR" & state=="KERALA"
replace block = "BALUSSERI" if block=="BNALUSSERY" & district=="KOZHIKODE" & state=="KERALA"
replace block = "CHELANNUR" if block=="CHELANNOOR" & district=="KOZHIKODE" & state=="KERALA"
replace block = "KUNNUMMAL" if block=="KUNNUMMEL" & district=="KOZHIKODE" & state=="KERALA"
replace block = "PANTHALAYANI" if block=="PANDALAYANI" & district=="KOZHIKODE" & state=="KERALA"
replace block = "PERAMBRA" if block=="PERMBRA" & district=="KOZHIKODE" & state=="KERALA"
replace block = "THUNERI" if block=="THOONERI" & district=="KOZHIKODE" & state=="KERALA"
replace block = "AREEKODE" if block=="AREECODE" & district=="MALAPPURAM" & state=="KERALA"
replace block = "KUTTIPPURAM" if block=="KUTTIPURAM" & district=="MALAPPURAM" & state=="KERALA"
replace block = "PERUMPADAPPA" if block=="PERUMPADAPPU" & district=="MALAPPURAM" & state=="KERALA"
replace block = "WANDOOR" if block=="VANDOOR" & district=="MALAPPURAM" & state=="KERALA"
replace block = "NEMMARA" if block=="NENMARA" & district=="PALAKKAD" & state=="KERALA"
replace block = "ELAMDESOM" if block=="ELAMDESAM" & district=="IDUKKI" & state=="KERALA"
replace block = "NEDUMKANDAM" if block=="NEDUMKANDOM" & district=="IDUKKI" & state=="KERALA"
replace block = "KADAMBATTUR" if block=="KADAMBATHUR" & district=="THIRUVALLUR" & state=="TAMIL NADU"
replace block = "R.K.PET" if block=="R. K. PET" & district=="THIRUVALLUR" & state=="TAMIL NADU"
replace block = "TIRUVELANGADU" if block=="THIRUVALANGADU" & district=="THIRUVALLUR" & state=="TAMIL NADU"
replace block = "ACHARAPAKKAM" if block=="ACHIRAPAKKAM" & district=="KANCHEEPURAM" & state=="TAMIL NADU"
replace block = "KATTANKOLATTUR" if block=="KATTANKULATHUR" & district=="KANCHEEPURAM" & state=="TAMIL NADU"
replace block = "THIRUPORUR" if block=="TIRUPORUR" & district=="KANCHEEPURAM" & state=="TAMIL NADU"
replace block = "K.V. KUPPAM" if block=="K V KUPPAM" & district=="VELLORE" & state=="TAMIL NADU"
replace block = "PERNAMPATTU" if block=="PERANAMBATTU" & district=="VELLORE" & state=="TAMIL NADU"
replace block = "THALY" if block=="THALLY" & district=="DHARMAPURI" & state=="TAMIL NADU"
replace block = "JAWATHU HILLS" if block=="JAWADHUHILLS" & district=="TIRUVANNAMALAI" & state=="TAMIL NADU"
replace block = "KILPERNNATHUR" if block=="KILPENNATHUR" & district=="TIRUVANNAMALAI" & state=="TAMIL NADU"
replace block = "CHINNA SALEM" if block=="CHINNASALEM" & district=="VILUPPURAM" & state=="TAMIL NADU"
replace block = "KALRAYAN HILLS" if block=="KALRAYANHILLS" & district=="VILUPPURAM" & state=="TAMIL NADU"
replace block = "KANAI" if block=="KANNAI" & district=="VILUPPURAM" & state=="TAMIL NADU"
replace block = "MELMALAYANUR" if block=="MELMALAIYANUR" & district=="VILUPPURAM" & state=="TAMIL NADU"
replace block = "VIRKRAVANDI" if block=="VIKRAVANDI" & district=="VILUPPURAM" & state=="TAMIL NADU"
replace block = "AYODHIAPATTINAM" if block=="AYOTHIAPATTINAM" & district=="SALEM" & state=="TAMIL NADU"
replace block = "NANGAVALLI" if block=="NAGAVALLI" & district=="SALEM" & state=="TAMIL NADU"
replace block = "PANAIMARATHUPATTI" if block=="PANAMARATHUPATTAI" & district=="SALEM" & state=="TAMIL NADU"
replace block = "PETHANAICKENPALAYAM" if block=="PETHANAIKENPALAYAM" & district=="SALEM" & state=="TAMIL NADU"
replace block = "TALAVASAL" if block=="THALAIVASAL" & district=="SALEM" & state=="TAMIL NADU"
replace block = "ELAICHIPALAYAM" if block=="ELACHIPALAYAM" & district=="NAMAKKAL" & state=="TAMIL NADU"
replace block = "KABILAMALAI" if block=="KABILARMALAI" & district=="NAMAKKAL" & state=="TAMIL NADU"
replace block = "VENNANTHUR" if block=="VENNATHUR" & district=="NAMAKKAL" & state=="TAMIL NADU"
replace block = "TALAVADI" if block=="THALAVADI" & district=="ERODE" & state=="TAMIL NADU"
replace block = "PERIYANAICKENPALAYAM" if block=="PERIYANAICKEN PALAYA" & district=="COIMBATORE" & state=="TAMIL NADU"
replace block = "GUZILIAMPARAI" if block=="GUZILLIAMPARAI" & district=="DINDIGUL" & state=="TAMIL NADU"
replace block = "SHANARPATTI" if block=="SANARPATTI" & district=="DINDIGUL" & state=="TAMIL NADU"
replace block = "UPPILIYAPURAM" if block=="UPPILLIAPURAM" & district=="TIRUCHIRAPPALLI" & state=="TAMIL NADU"
replace block = "KEELAIYUR" if block=="KEEZHAIYUR" & district=="NAGAPATTINAM" & state=="TAMIL NADU"
replace block = "KORADACHERI" if block=="KORADACHERRY" & district=="THIRUVARUR" & state=="TAMIL NADU"
replace block = "ANNAVASAL" if block=="ANNVASAL" & district=="PUDUKKOTTAI" & state=="TAMIL NADU"
replace block = "KUNNANDARKOVIL" if block=="KUNNANDARKOIL" & district=="PUDUKKOTTAI" & state=="TAMIL NADU"
replace block = "TIRUVARANKULAM" if block=="THIRUVARANKULAM" & district=="PUDUKKOTTAI" & state=="TAMIL NADU"
replace block = "KANNANKUDI" if block=="KANNAKUDI" & district=="SIVAGANGA" & state=="TAMIL NADU"
replace block = "S. PUDUR" if block=="S.PUDUR" & district=="SIVAGANGA" & state=="TAMIL NADU"
replace block = "SINGAMPUNERI" if block=="SINGAMPUNARY" & district=="SIVAGANGA" & state=="TAMIL NADU"
replace block = "KALLLIKUDI" if block=="KALLIKUDI" & district=="MADURAI" & state=="TAMIL NADU"
replace block = "T. KALLUPATTI" if block=="T.KALLUPATTI" & district=="MADURAI" & state=="TAMIL NADU"
replace block = "TIRUPULLANI" if block=="TIRUPPULLANI" & district=="RAMANATHAPURAM" & state=="TAMIL NADU"
replace block = "ALWARTHIRUNAGIRI" if block=="ALWARTHIRUNGIRI" & district=="THOOTHUKKUDI" & state=="TAMIL NADU"
replace block = "KARUNKULAM" if block=="KARUNGULAM" & district=="THOOTHUKKUDI" & state=="TAMIL NADU"
replace block = "KURUNTHANCODE" if block=="KURUNTHENCODE" & district=="KANNIYAKUMARI" & state=="TAMIL NADU"
replace block = "THACKALAI" if block=="THAKKALAY" & district=="KANNIYAKUMARI" & state=="TAMIL NADU"
replace block = "PERINTHALMANNA" if block=="PERUMTHALMANNA" & district=="MALAPPURAM" & state=="KERALA"
replace block = "LOHIAN KHAS" if block=="LOHIAN" & district=="JALANDHAR" & state=="PUNJAB"
replace block = "SIRHIND" if block=="SIRHIND MANDI" & district=="FATEHGARH SAHIB" & state=="PUNJAB"
replace block = "SADAURA" if block=="SADAURA (PART)" & district=="YAMUNANAGAR" & state=="HARYANA"
replace block = "BAKANI FOREST VILLAGE" if block=="BAKANI" & district=="JHALAWAR" & state=="RAJASTHAN"
replace block = "BUDHANPUR SEOHARA" if block=="BUDHANPUR" & district=="BIJNOR" & state=="UTTAR PRADESH"
replace block = "HALDAUR (KHARI JHALU)" if block=="HALDAUR" & district=="BIJNOR" & state=="UTTAR PRADESH"
replace block = "JAWAN SIKANDERPUR" if block=="JAWAN" & district=="ALIGARH" & state=="UTTAR PRADESH"
replace block = "KATRA (KHUDAGANJ)" if block=="KATRA" & district=="SHAHJAHANPUR" & state=="UTTAR PRADESH"
replace block = "CHAMBANG" if block=="CHAMBANG" & district=="KURUNG KUMEY" & state=="ARUNACHAL PRADESH"
replace block = "MAHRONI" if block=="MAHRAUNI" & district=="LALITPUR" & state=="UTTAR PRADESH"
replace block = "VALLABHNAGAR" if block=="VALLABHNA" & district=="UDAIPUR" & state=="RAJASTHAN"
replace block = "THUAMUL RAMPUR" if block=="TH.RAMPUR" & district=="KALAHANDI" & state=="ORISSA"
replace block = "MADANPUR RAMPUR" if block=="M.RAMPUR" & district=="KALAHANDI" & state=="ORISSA"
replace block = "HARRAI" if block=="HARAI" & district=="CHHINDWARA" & state=="MADHYA PRADESH"
replace block = "BAIDHAN" if block=="WAIDHAN" & district=="SIDHI" & state=="MADHYA PRADESH"
replace block = "LACHHMANGARH" if block=="LAXMANGARH" & district=="ALWAR" & state=="RAJASTHAN"
replace block = "SATNA (SOHAWAL)" if block=="SOHAWAL" & district=="SATNA" & state=="MADHYA PRADESH"
replace block = "KENDUJHARGARH" if block=="KEONJHAR" & district=="KENDUJHAR" & state=="ORISSA"
replace block = "POUNDI-UPRODA" if block=="PODI UPARODA" & district=="KORBA" & state=="CHHATTISGARH"
replace block = "ADITYAPUR(GAMHARIA)" if block=="GAMHARIA" & district=="PASHCHIMI SINGHBHUM" & state=="JHARKHAND"
replace block = "MALHARGARH" if block=="MLAHARGRAH" & district=="MANDSAUR" & state=="MADHYA PRADESH"
replace block = "HOSHIARPUR I" if block=="HOSHIARPUR 1" & district=="HOSHIARPUR" & state=="PUNJAB"
replace block = "GOBINDPUR(RAJNAGAR)" if block=="RAJNAGAR" & district=="PASHCHIMI SINGHBHUM" & state=="JHARKHAND"
replace block = "GADARWARA" if block=="BABAI CHICHLI" & district=="NARSIMHAPUR" & state=="MADHYA PRADESH"
replace block = "BHANDARI POKHARI" if block=="BANDARIPOKHARI" & district=="BHADRAK" & state=="ORISSA"
replace block = "PICHHORE" if block=="DABRA" & district=="GWALIOR" & state=="MADHYA PRADESH"
replace block = "BARSAHI" if block=="BADSAHI" & district=="MAYURBHANJ" & state=="ORISSA"
replace block = "NARAIN BAGAR" if block=="NARYANBAGAR" & district=="CHAMOLI" & state=="UTTARAKHAND"
replace block = "KOULDHAR" if block=="THAULDHAR" & district=="TEHRI GARHWAL" & state=="UTTARAKHAND"
replace block = "KESHORAI PATAN" if block=="KESHORAIP" & district=="BUNDI" & state=="RAJASTHAN"
replace block = "KISHANGARH" if block=="SILORA" & district=="AJMER" & state=="RAJASTHAN"
replace block = "BAHANAGA" if block=="BHANAGA" & district=="BALESHWAR" & state=="ORISSA"
replace block = "GIRD" if block=="GHATIGAON" & district=="GWALIOR" & state=="MADHYA PRADESH"
replace block = "KESHORAIPATAN" if block=="KESHORAIP" & district=="BUNDI" & state=="RAJASTHAN"
replace block = "HOSHIARPUR" if block=="HOSHIARPUR 2" & district=="HOSHIARPUR" & state=="PUNJAB"
replace block = "BADAMBA" if block=="BARAMBA" & district=="CUTTACK" & state=="ORISSA"
replace block = "ROING" if block=="ROING-KORONU" & district=="DIBANG VALLEY" & state=="ARUNACHAL PRADESH"
replace block = "RAJIM(PHINGESHWAR)" if block=="FINGESHWAR" & district=="RAIPUR" & state=="CHHATTISGARH"
replace block = "MALKHARODA" if block=="MALKAHARUADA" & district=="JANJGIR - CHAMPA" & state=="CHHATTISGARH"
replace block = "BARAILY" if block=="BARELI (BADI)" & district=="RAISEN" & state=="MADHYA PRADESH"
replace block = "MAHUADANR" if block=="MAHUADAR" & district=="PALAMU" & state=="JHARKHAND"
replace block = "BEHAT" if block=="SADHAULI KADEEM" & district=="SAHARANPUR" & state=="UTTAR PRADESH"
replace block = "KHADGANVA" if block=="KHADGOWAN" & district=="KORIYA" & state=="CHHATTISGARH"
replace block = "PAKAUR" if block=="PAKUR" & district=="PAKAUR" & state=="JHARKHAND"
replace block = "LATEHAR" if block=="LATEHAR" & district=="PALAMU" & state=="JHARKHAND"
replace block = "MAWANA" if block=="HASTINAPUR" & district=="MEERUT" & state=="UTTAR PRADESH"
replace block = "PUSOUR" if block=="PUSSORE" & district=="RAIGARH" & state=="CHHATTISGARH"
replace block = "DEORIA" if block=="TARKULWA" & district=="DEORIA" & state=="UTTAR PRADESH"
replace block = "GOGAON" if block=="GOGANWA" & district=="WEST NIMAR" & state=="MADHYA PRADESH"
replace block = "ZAHRIKHAL" if block=="JAIRIKHAL" & district=="GARHWAL" & state=="UTTARAKHAND"
replace block = "KARNAPRAYAG" if block=="KARANPRYAG" & district=="CHAMOLI" & state=="UTTARAKHAND"
replace block = "KAVATHE-MAHANKAL" if block=="KAVATHE-MAHAN" & district=="SANGLI" & state=="MAHARASHTRA"
replace block = "NUAON" if block=="NOAWAN" & district=="KAIMUR (BHABUA)" & state=="BIHAR"
replace block = "TIRUVANNAMALAI" if block=="THIRUVANNAMALAI" & district=="TIRUVANNAMALAI" & state=="TAMIL NADU"
replace block = "MODINAGAR" if block=="MURADNAGAR" & district=="GHAZIABAD" & state=="UTTAR PRADESH"
replace block = "NUAGAON" if block=="K.NUAGAN" & district=="KANDHAMAL" & state=="ORISSA"
replace block = "ANAKAVUR" if block=="ANAKKAVOOR" & district=="TIRUVANNAMALAI" & state=="TAMIL NADU"
replace block = "MANDAWARA" if block=="MADAVRA" & district=="LALITPUR" & state=="UTTAR PRADESH"
replace block = "HAWAI" if block=="HAWAI-WALONG" & district=="LOHIT" & state=="ARUNACHAL PRADESH"
replace block = "KOLORIANG" if block=="KOLORIANG-SARLI" & district=="LOWER SUBANSIRI" & state=="ARUNACHAL PRADESH"
replace block = "NANDGAON-KHANDESHWAR" if block=="NANDGAON-KHAN" & district=="AMRAVATI" & state=="MAHARASHTRA"
replace block = "PAKURIA" if block=="PAKUR" & district=="PAKAUR" & state=="JHARKHAND"
replace block = "KEOTIRANWAY" if block=="KEOTI" & district=="DARBHANGA" & state=="BIHAR"
*replace block = "JAMAI" if block=="HARAI" & district=="CHHINDWARA" & state=="MADHYA PRADESH"
*replace block = "HOSHIARPUR" if block=="HOSHIARPUR 1" & district=="HOSHIARPUR" & state=="PUNJAB"
*replace block = "MALERKOTLA" if block=="MALERKOTLA 1" & district=="SANGRUR" & state=="PUNJAB"
*replace block = "NANDIGRAM III" if block=="NANDIGRAM IIII" & district=="MEDINIPUR" & state=="WEST BENGAL"
*replace block = "HOSHIARPUR" if block=="HOSHIARPUR 2" & district=="HOSHIARPUR" & state=="PUNJAB"
*replace block = "HUZUR" if block=="PHANDA" & district=="BHOPAL" & state=="MADHYA PRADESH"
*replace block = "GOHARGANJ" if block=="OBEDULLAGANJ" & district=="RAISEN" & state=="MADHYA PRADESH"
*replace block = "RAISEN" if block=="SANCHI" & district=="RAISEN" & state=="MADHYA PRADESH"
*replace block = "GIRD" if block=="MORAR" & district=="GWALIOR" & state=="MADHYA PRADESH"
*replace block = "BIJNOR" if block=="MO. PURDEWAL" & district=="BIJNOR" & state=="UTTAR PRADESH"
*replace block = "RUPNAGAR" if block=="ROPAR" & district=="RUPNAGAR" & state=="PUNJAB"
*replace block = "PITHORAGARH" if block=="BIN" & district=="PITHORAGARH" & state=="UTTARAKHAND"
*replace block = "ROBERTSGANJ" if block=="NAVGAON" & district=="SONBHADRA" & state=="UTTAR PRADESH"
*replace block = "NAKUR" if block=="GANGOH" & district=="SAHARANPUR" & state=="UTTAR PRADESH"
*replace block = "GADARWARA" if block=="SAINKHEDA (GADARWAD)" & district=="NARSIMHAPUR" & state=="MADHYA PRADESH"
*replace block = "DHAMPUR" if block=="AFAJALGANJ(KASAMPUR GARHI)" & district=="BIJNOR" & state=="UTTAR PRADESH"
*replace block = "HATHRAS" if block=="MURSAN" & district=="HATHRAS" & state=="UTTAR PRADESH"
*replace block = "MULTAI" if block=="P PATTAN" & district=="BETUL" & state=="MADHYA PRADESH"
*replace block = "DHAMPUR" if block=="ALHEPUR" & district=="BIJNOR" & state=="UTTAR PRADESH"
*replace block = "BEHAT" if block=="MUJAFFARABAD" & district=="SAHARANPUR" & state=="UTTAR PRADESH"
*replace block = "AONLA" if block=="JAFRABAD" & district=="BAREILLY" & state=="UTTAR PRADESH"
*replace block = "SAHARANPUR" if block=="PUNWARAKA" & district=="SAHARANPUR" & state=="UTTAR PRADESH"
*replace block = "ITARSI" if block=="KEISLA" & district=="HOSHANGABAD" & state=="MADHYA PRADESH"
*replace block = "TUSHURA" if block=="TENTULIKHURI" & district=="BALANGIR" & state=="ORISSA"
*replace block = "SITAPUR" if block=="EALLIYA" & district=="SITAPUR" & state=="UTTAR PRADESH"
*replace block = "KANPUR" if block=="BIDHANU" & district=="KANPUR NAGAR" & state=="UTTAR PRADESH"
*replace block = "SAHARANPUR" if block=="SARSAWAN" & district=="SAHARANPUR" & state=="UTTAR PRADESH"
*replace block = "RAIPUR" if block=="DHARSIWA" & district=="RAIPUR" & state=="CHHATTISGARH"
*replace block = "KANPUR" if block=="SARSAUL" & district=="KANPUR NAGAR" & state=="UTTAR PRADESH"
*replace block = "SAHARANPUR" if block=="BANIAKHERA" & district=="SAHARANPUR" & state=="UTTAR PRADESH"
*replace block = "SOTHA" if block=="SANTHA" & district=="SANT KABIR NAGAR" & state=="UTTAR PRADESH"
*replace block = "GHAZIPUR" if block=="KARANDA" & district=="GHAZIPUR" & state=="UTTAR PRADESH"
*replace block = "KAIRANA" if block=="UN" & district=="MUZAFFARNAGAR" & state=="UTTAR PRADESH"
*replace block = "MATHURA" if block=="PHARAH" & district=="MATHURA" & state=="UTTAR PRADESH"
}


replace village = upper(trim(itrim(village)))
replace village = "-" if village==""

gen st_code=.
replace st_code = 28 if state=="ANDHRA PRADESH"
replace st_code = 12 if state=="ARUNACHAL PRADESH"
replace st_code = 18 if state=="ASSAM"
replace st_code = 10 if state=="BIHAR"
replace st_code = 22 if state=="CHHATTISGARH"
replace st_code = 24 if state=="GUJARAT"
replace st_code = 6  if state=="HARYANA"
replace st_code = 2  if state=="HIMACHAL PRADESH"
replace st_code = 1  if state=="JAMMU & KASHMIR"
replace st_code = 20 if state=="JHARKHAND"
replace st_code = 29 if state=="KARNATAKA"
replace st_code = 32 if state=="KERALA"
replace st_code = 23 if state=="MADHYA PRADESH"
replace st_code = 27 if state=="MAHARASHTRA"
replace st_code = 14 if state=="MANIPUR"
replace st_code = 17 if state=="MEGHALAYA"
replace st_code = 15 if state=="MIZORAM"
replace st_code = 13 if state=="NAGALAND"
replace st_code = 21 if state=="ORISSA"
replace st_code = 3  if state=="PUNJAB"
replace st_code = 8  if state=="RAJASTHAN"
replace st_code = 11 if state=="SIKKIM"
replace st_code = 33 if state=="TAMIL NADU"
replace st_code = 16 if state=="TRIPURA"
replace st_code = 9  if state=="UTTAR PRADESH"
replace st_code = 5  if state=="UTTARAKHAND"
replace st_code = 19 if state=="WEST BENGAL"
*replace st_code = 4  if state=="CHANDIGARH"
*replace st_code = 7  if state=="DEHLI"
*replace st_code = 25 if state=="DAMAN AND DIU"
*replace st_code = 26 if state=="DADRA AND NAGAR HAVELI"
*replace st_code = 30 if state=="GOA"
*replace st_code = 31 if state=="LAKSHDWEEP"
*replace st_code = 34 if state=="PONDICHERRY"
*replace st_code = 35 if state=="ANDAMAN AND NICOBAR ISLANDS"
drop if st_code ==.
sort st_code district
duplicates drop
gen h3_id = _n
drop v1
order state district block village habitation
egen h3v_id = group(st_code state district block village)
assert h3v_id !=.
duplicates drop
compress
save "$hab3/hab_census03_for_merge_all.dta", replace
collapse (sum) totp_h3=totp (max) maxp_h3=totp (count) count_h3=h3_id, by(st_code state district block village h3v_id)
compress
save "$hab3/hab_census03_for_merge.dta", replace
}

********************************************************************************
********************************************************************************

** Step 3: Make sure all states & districts have exact matches, copy dt_codes
{
use "$hab3/hab_census03_for_merge.dta", clear
keep state st_code district 
duplicates drop
merge 1:m state st_code district using "$hmerge/pca_2001_names_merges_all.dta", gen(merge_dt)  // try PCA 2001 district name
keep state st_code district dt_code merge_dt
duplicates drop 
sort state district
drop if merge_dt==2
merge 1:m state st_code district using "$hab3/hab_census03_for_merge.dta", nogen
order st_code state dt_code district merge_dt
sort  st_code state dt_code district merge_dt
assert dt_code!=. & dt_code>0 if merge_dt==3
save "$hab3/hab_census03_for_merge.dta", replace

keep state st_code district dt_code merge_dt // try PCA 2011 district name
duplicates drop
rename district district11
rename dt_code dt_code_hab
merge 1:m state st_code district11 using "$hmerge/pca_2001_names_merges_all.dta", gen(merge_dt11) keepusing(district dt_code) 
duplicates drop
replace dt_code_hab = dt_code if dt_code_hab==. & merge_dt11==3
drop if merge_dt11==2
drop district dt_code
rename district11 district
duplicates drop
merge 1:m state st_code district using "$hab3/hab_census03_for_merge.dta", nogen
replace dt_code = dt_code_hab if dt_code==. & dt_code_hab!=.
drop dt_code_hab
duplicates drop
save "$hab3/hab_census03_for_merge.dta", replace

keep state st_code district dt_code merge_dt merge_dt11 // try RGGVY district name
duplicates drop
rename district district_rggvy
rename dt_code dt_code_hab
merge 1:m state st_code district_rggvy using "$hmerge/pca_2001_names_merges_all.dta", gen(merge_dtrggvy) keepusing(district dt_code) 
duplicates drop
replace dt_code_hab = dt_code if dt_code_hab==. & merge_dtrggvy==3
drop if merge_dtrggvy==2
drop district dt_code
rename district_rggvy district
duplicates drop
merge 1:m state st_code district using "$hab3/hab_census03_for_merge.dta", nogen
replace dt_code = dt_code_hab if dt_code==. & dt_code_hab!=.
drop dt_code_hab
duplicates drop
save "$hab3/hab_census03_for_merge.dta", replace

keep state st_code district dt_code merge_dt merge_dt11 merge_dtrggvy // try RGGVY habitation list district name
duplicates drop
rename district district_lh
rename dt_code dt_code_hab
merge 1:m state st_code district_lh using "$hmerge/pca_2001_names_merges_all.dta", gen(merge_dtlh) keepusing(district dt_code) 
duplicates drop
replace dt_code_hab = dt_code if dt_code_hab==. & merge_dtlh==3
drop if merge_dtlh==2
drop district dt_code
rename district_lh district
duplicates drop
merge 1:m state st_code district using "$hab3/hab_census03_for_merge.dta", nogen
replace dt_code = dt_code_hab if dt_code==. & dt_code_hab!=.
drop dt_code_hab
duplicates drop
sort st_code dt_code h3v_id
order state st_code district dt_code block village 
save "$hab3/hab_census03_for_merge.dta", replace
}

********************************************************************************
********************************************************************************

** Step 4: Make sure all states & districts & blocks have exact matches, copy bk_codes
{
use "$hab3/hab_census03_for_merge.dta", clear
keep state st_code district dt_code block merge*
duplicates drop
merge 1:m state st_code district dt_code block using "$hmerge/pca_2001_names_merges_all.dta", gen(merge_bk)
keep state st_code district dt_code block bk_code_pca merge*
duplicates drop 
sort state district block
/*
keep if merge_bk<3 
gen block_name_merge = ""  // create field for merge name, to go back and replace when insheeting habitation census
gen q = `"""'

*Iterate 2x to correct blocks that are 1-2 letters off
gen block_pca_above = block[_n-1] if merge_bk[_n-1]==2
gen block_pca_below = block[_n+1] if merge_bk[_n+1]==2
strdist block block_pca_above if merge_bk==1, gen(dist_above)
strdist block block_pca_below if merge_bk==1, gen(dist_below)
br block block_pca_above dist_above if dist_above<3
br block block_pca_below dist_below if dist_below<3
replace block_name_merge = block_pca_above if merge_bk==1 & merge_bk[_n-1]==2 & dist_above<3 & dist_below>3 & state==state[_n-1] & district==district[_n-1] & merge_dt==3 
replace block_name_merge = block_pca_below if merge_bk==1 & merge_bk[_n+1]==2 & dist_below<3 & dist_above>3 & state==state[_n+1] & district==district[_n+1] & merge_dt==3 
gen replace_text = "replace block = " + q + block_name_merge + q + " if block==" + q + block + q + " & district==" + q + district + q + " & state==" + q + state + q if block_name_merge!="" & word(block_name_merge,-1)!="I" & word(block_name_merge,-1)!="II" & word(block_name_merge,-1)!="III" & word(block,-1)!="I" & word(block,-1)!="II" & word(block,-1)!="III"
br if replace_text!=""

*Iterate 1x to correct blocks that have a matching word
gen block_pca_above = block[_n-1] if merge_bk[_n-1]==2
gen block_pca_below = block[_n+1] if merge_bk[_n+1]==2
gen block_name_merge_above = block_pca_above if merge_bk==1 & merge_bk[_n-1]==2 & word(block,1)==word(block_pca_above,1) & state==state[_n-1] & district==district[_n-1] & merge_dt==3 
gen block_name_merge_below = block_pca_below if merge_bk==1 & merge_bk[_n+1]==2 & word(block,1)==word(block_pca_below,1) & state==state[_n+1] & district==district[_n+1] & merge_dt==3 
br state district block block_pca* block_name_merge_* if block_name_merge_above!="" | block_name_merge_below!=""
replace block_name_merge = block_name_merge_above if block_name_merge=="" & word(block_name_merge_above,-1)!="I" & word(block_name_merge_above,-1)!="II" & word(block_name_merge_above,-1)!="III" & word(block_name_merge_above,-1)!="WEST" & word(block_name_merge_above,-1)!="EAST" & word(block_name_merge_above,-1)!="SOUTH" & word(block_name_merge_above,-1)!="NORTH" & word(block,-1)!="I" & word(block,-1)!="II" & word(block,-1)!="III" & word(block,-1)!="WEST" & word(block,-1)!="EAST" & word(block,-1)!="SOUTH" & word(block,-1)!="NORTH"  
replace block_name_merge = block_name_merge_below if block_name_merge=="" & word(block_name_merge_below,-1)!="I" & word(block_name_merge_below,-1)!="II" & word(block_name_merge_below,-1)!="III" & word(block_name_merge_below,-1)!="WEST" & word(block_name_merge_below,-1)!="EAST" & word(block_name_merge_below,-1)!="SOUTH" & word(block_name_merge_below,-1)!="NORTH" & word(block,-1)!="I" & word(block,-1)!="II" & word(block,-1)!="III" & word(block,-1)!="WEST" & word(block,-1)!="EAST" & word(block,-1)!="SOUTH" & word(block,-1)!="NORTH"  
gen replace_text = "replace block = " + q + block_name_merge + q + " if block==" + q + block + q + " & district==" + q + district + q + " & state==" + q + state + q if block_name_merge!="" & word(block_name_merge,-1)!="I" & word(block_name_merge,-1)!="II" & word(block_name_merge,-1)!="III" & word(block,-1)!="I" & word(block,-1)!="II" & word(block,-1)!="III"
br if replace_text!=""
*/
drop if merge_bk==2
replace bk_code = . if block=="SIRPUR" & district=="ADILABAD" & state=="ANDHRA PRADESH"
duplicates drop
merge 1:m state st_code district dt_code block using "$hab3/hab_census03_for_merge.dta", nogen
order st_code state dt_code district block bk_code_pca
sort  st_code state dt_code district block bk_code_pca
save "$hab3/hab_census03_for_merge.dta", replace



use "$hab3/hab_census03_for_merge.dta", clear // try PCA 2011 block names
keep st_code state dt_code district block merge*
rename block block11
duplicates drop
merge 1:m st_code state dt_code district block11 using "$hmerge/pca_2001_names_merges_all.dta", gen(merge_bk11)
keep st_code state dt_code district block11 bk_code11 merge_bk merge_bk11
sort st_code dt_code block11
replace state = state[_n-1] if state=="" & st_code==st_code[_n-1]
replace state = state[_n+1] if state=="" & st_code==st_code[_n+1]
replace district = district[_n-1] if district=="" & dt_code==dt_code[_n-1]
replace district = district[_n+1] if district=="" & dt_code==dt_code[_n+1]
duplicates drop 
/*
keep if merge_bk11<3 & merge_bk!=3
drop if block11==""
gen block_name_merge = ""  // create field for merge name, to go back and replace when insheeting habitation census
gen q = `"""'

*Iterate 1x to correct blocks that are 1-2 letters off
gen block_pca_above = block11[_n-1] if merge_bk11[_n-1]==2
gen block_pca_below = block11[_n+1] if merge_bk11[_n+1]==2
strdist block11 block_pca_above if merge_bk11==1, gen(dist_above)
strdist block11 block_pca_below if merge_bk11==1, gen(dist_below)
br block11 block_pca_above dist_above if dist_above<3
br block11 block_pca_below dist_below if dist_below<3
replace block_name_merge = block_pca_above if merge_bk11==1 & merge_bk11[_n-1]==2 & dist_above<3 & dist_below>3 & state==state[_n-1] & district==district[_n-1] 
replace block_name_merge = block_pca_below if merge_bk11==1 & merge_bk11[_n+1]==2 & dist_below<3 & dist_above>3 & state==state[_n+1] & district==district[_n+1] 
gen replace_text = "replace block = " + q + block_name_merge + q + " if block==" + q + block11 + q + " & district==" + q + district + q + " & state==" + q + state + q if block_name_merge!="" & word(block_name_merge,-1)!="I" & word(block_name_merge,-1)!="II" & word(block_name_merge,-1)!="III" & word(block11,-1)!="I" & word(block11,-1)!="II" & word(block11,-1)!="III"
br if replace_text!=""

*Iterate 1x to correct blocks that have a matching word
gen block_pca_above = block11[_n-1] if merge_bk11[_n-1]==2
gen block_pca_below = block11[_n+1] if merge_bk11[_n+1]==2
gen block_name_merge_above = block_pca_above if merge_bk11==1 & merge_bk11[_n-1]==2 & word(block11,1)==word(block_pca_above,1) & state==state[_n-1] & district==district[_n-1] 
gen block_name_merge_below = block_pca_below if merge_bk11==1 & merge_bk11[_n+1]==2 & word(block11,1)==word(block_pca_below,1) & state==state[_n+1] & district==district[_n+1] 
br state district block11 block_pca* block_name_merge_* if block_name_merge_above!="" | block_name_merge_below!=""
replace block_name_merge = block_name_merge_above if block_name_merge=="" & word(block_name_merge_above,-1)!="I" & word(block_name_merge_above,-1)!="II" & word(block_name_merge_above,-1)!="III" & word(block_name_merge_above,-1)!="WEST" & word(block_name_merge_above,-1)!="EAST" & word(block_name_merge_above,-1)!="SOUTH" & word(block_name_merge_above,-1)!="NORTH" & word(block11,-1)!="I" & word(block11,-1)!="II" & word(block11,-1)!="III" & word(block11,-1)!="WEST" & word(block11,-1)!="EAST" & word(block11,-1)!="SOUTH" & word(block11,-1)!="NORTH"  
replace block_name_merge = block_name_merge_below if block_name_merge=="" & word(block_name_merge_below,-1)!="I" & word(block_name_merge_below,-1)!="II" & word(block_name_merge_below,-1)!="III" & word(block_name_merge_below,-1)!="WEST" & word(block_name_merge_below,-1)!="EAST" & word(block_name_merge_below,-1)!="SOUTH" & word(block_name_merge_below,-1)!="NORTH" & word(block11,-1)!="I" & word(block11,-1)!="II" & word(block11,-1)!="III" & word(block11,-1)!="WEST" & word(block11,-1)!="EAST" & word(block11,-1)!="SOUTH" & word(block11,-1)!="NORTH"  
gen replace_text = "replace block = " + q + block_name_merge + q + " if block==" + q + block11 + q + " & district==" + q + district + q + " & state==" + q + state + q if block_name_merge!="" & word(block_name_merge,-1)!="I" & word(block_name_merge,-1)!="II" & word(block_name_merge,-1)!="III" & word(block11,-1)!="I" & word(block11,-1)!="II" & word(block11,-1)!="III"
br if replace_text!=""
*/
drop if merge_bk11==2
duplicates drop
rename block11 block
replace bk_code11 = . if block=="SIRPUR" & district=="ADILABAD" & state=="ANDHRA PRADESH"
merge m:m state st_code district dt_code block using "$hab3/hab_census03_for_merge.dta", nogen
order st_code state dt_code district block bk_code_pca bk_code11
sort  st_code state dt_code district block bk_code_pca bk_code11
save "$hab3/hab_census03_for_merge.dta", replace



use "$hab3/hab_census03_for_merge.dta", clear // try Concordance 2001 block names
keep st_code state dt_code district block merge*
rename block block_2001
duplicates drop
merge 1:m st_code state dt_code district block_2001 using "$hmerge/pca_2001_names_merges_all.dta", gen(merge_bkconc01)
keep st_code state dt_code district block_2001 bk_code_conc01 merge_bk merge_bk11 merge_bkconc01
sort st_code dt_code block_2001
replace state = state[_n-1] if state=="" & st_code==st_code[_n-1]
replace state = state[_n+1] if state=="" & st_code==st_code[_n+1]
replace district = district[_n-1] if district=="" & dt_code==dt_code[_n-1]
replace district = district[_n+1] if district=="" & dt_code==dt_code[_n+1]
duplicates drop 
/*
keep if merge_bkconc01<3 & merge_bk!=3 & merge_bk11!=3
drop if block_2001==""
gen block_name_merge = ""  // create field for merge name, to go back and replace when insheeting habitation census
gen q = `"""'

*Iterate 1x to correct blocks that are 1-2 letters off
gen block_pca_above = block_2001[_n-1] if merge_bkconc01[_n-1]==2
gen block_pca_below = block_2001[_n+1] if merge_bkconc01[_n+1]==2
strdist block_2001 block_pca_above if merge_bkconc01==1, gen(dist_above)
strdist block_2001 block_pca_below if merge_bkconc01==1, gen(dist_below)
br block_2001 block_pca_above dist_above if dist_above<3
br block_2001 block_pca_below dist_below if dist_below<3
replace block_name_merge = block_pca_above if merge_bkconc01==1 & merge_bkconc01[_n-1]==2 & dist_above<3 & dist_below>3 & state==state[_n-1] & district==district[_n-1] 
replace block_name_merge = block_pca_below if merge_bkconc01==1 & merge_bkconc01[_n+1]==2 & dist_below<3 & dist_above>3 & state==state[_n+1] & district==district[_n+1] 
gen replace_text = "replace block = " + q + block_name_merge + q + " if block==" + q + block_2001 + q + " & district==" + q + district + q + " & state==" + q + state + q if block_name_merge!="" & word(block_name_merge,-1)!="I" & word(block_name_merge,-1)!="II" & word(block_name_merge,-1)!="III" & word(block_2001,-1)!="I" & word(block_2001,-1)!="II" & word(block_2001,-1)!="III"
br if replace_text!=""

*Iterate 0x to correct blocks that have a matching word
gen block_pca_above = block_2001[_n-1] if merge_bkconc01[_n-1]==2
gen block_pca_below = block_2001[_n+1] if merge_bkconc01[_n+1]==2
gen block_name_merge_above = block_pca_above if merge_bkconc01==1 & merge_bkconc01[_n-1]==2 & word(block_2001,1)==word(block_pca_above,1) & state==state[_n-1] & district==district[_n-1] 
gen block_name_merge_below = block_pca_below if merge_bkconc01==1 & merge_bkconc01[_n+1]==2 & word(block_2001,1)==word(block_pca_below,1) & state==state[_n+1] & district==district[_n+1] 
br state district block_2001 block_pca* block_name_merge_* if block_name_merge_above!="" | block_name_merge_below!=""
replace block_name_merge = block_name_merge_above if block_name_merge=="" & word(block_name_merge_above,-1)!="I" & word(block_name_merge_above,-1)!="II" & word(block_name_merge_above,-1)!="III" & word(block_name_merge_above,-1)!="WEST" & word(block_name_merge_above,-1)!="EAST" & word(block_name_merge_above,-1)!="SOUTH" & word(block_name_merge_above,-1)!="NORTH" & word(block_2001,-1)!="I" & word(block_2001,-1)!="II" & word(block_2001,-1)!="III" & word(block_2001,-1)!="WEST" & word(block_2001,-1)!="EAST" & word(block_2001,-1)!="SOUTH" & word(block_2001,-1)!="NORTH"  
replace block_name_merge = block_name_merge_below if block_name_merge=="" & word(block_name_merge_below,-1)!="I" & word(block_name_merge_below,-1)!="II" & word(block_name_merge_below,-1)!="III" & word(block_name_merge_below,-1)!="WEST" & word(block_name_merge_below,-1)!="EAST" & word(block_name_merge_below,-1)!="SOUTH" & word(block_name_merge_below,-1)!="NORTH" & word(block_2001,-1)!="I" & word(block_2001,-1)!="II" & word(block_2001,-1)!="III" & word(block_2001,-1)!="WEST" & word(block_2001,-1)!="EAST" & word(block_2001,-1)!="SOUTH" & word(block_2001,-1)!="NORTH"  
gen replace_text = "replace block = " + q + block_name_merge + q + " if block==" + q + block_2001 + q + " & district==" + q + district + q + " & state==" + q + state + q if block_name_merge!="" & word(block_name_merge,-1)!="I" & word(block_name_merge,-1)!="II" & word(block_name_merge,-1)!="III" & word(block_2001,-1)!="I" & word(block_2001,-1)!="II" & word(block_2001,-1)!="III"
br if replace_text!=""
*/
drop if merge_bkconc01==2
duplicates drop
rename block_2001 block
replace bk_code_conc01 = . if block=="SIRPUR" & district=="ADILABAD" & state=="ANDHRA PRADESH"
merge m:m state st_code district dt_code block using "$hab3/hab_census03_for_merge.dta", nogen
order st_code state dt_code district block bk_code_pca bk_code11 bk_code_conc01
sort  st_code state dt_code district block bk_code_pca bk_code11 bk_code_conc01
save "$hab3/hab_census03_for_merge.dta", replace



use "$hab3/hab_census03_for_merge.dta", clear // try Concordance 2011 block names
keep st_code state dt_code district block merge*
rename block block_2011
duplicates drop
merge 1:m st_code state dt_code district block_2011 using "$hmerge/pca_2001_names_merges_all.dta", gen(merge_bkconc11)
keep st_code state dt_code district block_2011 bk_code_conc11 merge_bk merge_bk11 merge_bkconc01 merge_bkconc11
sort st_code dt_code block_2011
replace state = state[_n-1] if state=="" & st_code==st_code[_n-1]
replace state = state[_n+1] if state=="" & st_code==st_code[_n+1]
replace district = district[_n-1] if district=="" & dt_code==dt_code[_n-1]
replace district = district[_n+1] if district=="" & dt_code==dt_code[_n+1]
duplicates drop 
/*
keep if merge_bkconc11<3 & merge_bk!=3 & merge_bk11!=3 & merge_bkconc01!=3
drop if block_2011==""
gen block_name_merge = ""  // create field for merge name, to go back and replace when insheeting habitation census
gen q = `"""'

*Iterate 1x to correct blocks that are 1-2 letters off
gen block_pca_above = block_2011[_n-1] if merge_bkconc11[_n-1]==2
gen block_pca_below = block_2011[_n+1] if merge_bkconc11[_n+1]==2
strdist block_2011 block_pca_above if merge_bkconc11==1, gen(dist_above)
strdist block_2011 block_pca_below if merge_bkconc11==1, gen(dist_below)
br block_2011 block_pca_above dist_above if dist_above<3
br block_2011 block_pca_below dist_below if dist_below<3
replace block_name_merge = block_pca_above if merge_bkconc11==1 & merge_bkconc11[_n-1]==2 & dist_above<3 & dist_below>3 & state==state[_n-1] & district==district[_n-1] 
replace block_name_merge = block_pca_below if merge_bkconc11==1 & merge_bkconc11[_n+1]==2 & dist_below<3 & dist_above>3 & state==state[_n+1] & district==district[_n+1] 
gen replace_text = "replace block = " + q + block_name_merge + q + " if block==" + q + block_2011 + q + " & district==" + q + district + q + " & state==" + q + state + q if block_name_merge!="" & word(block_name_merge,-1)!="I" & word(block_name_merge,-1)!="II" & word(block_name_merge,-1)!="III" & word(block_2011,-1)!="I" & word(block_2011,-1)!="II" & word(block_2011,-1)!="III"
br if replace_text!=""

*Iterate 1x to correct blocks that have a matching word
gen block_pca_above = block_2011[_n-1] if merge_bkconc11[_n-1]==2
gen block_pca_below = block_2011[_n+1] if merge_bkconc11[_n+1]==2
gen block_name_merge_above = block_pca_above if merge_bkconc11==1 & merge_bkconc11[_n-1]==2 & word(block_2011,1)==word(block_pca_above,1) & state==state[_n-1] & district==district[_n-1] 
gen block_name_merge_below = block_pca_below if merge_bkconc11==1 & merge_bkconc11[_n+1]==2 & word(block_2011,1)==word(block_pca_below,1) & state==state[_n+1] & district==district[_n+1] 
br state district block_2011 block_pca* block_name_merge_* if block_name_merge_above!="" | block_name_merge_below!=""
replace block_name_merge = block_name_merge_above if block_name_merge=="" & word(block_name_merge_above,-1)!="I" & word(block_name_merge_above,-1)!="II" & word(block_name_merge_above,-1)!="III" & word(block_name_merge_above,-1)!="WEST" & word(block_name_merge_above,-1)!="EAST" & word(block_name_merge_above,-1)!="SOUTH" & word(block_name_merge_above,-1)!="NORTH" & word(block_2011,-1)!="I" & word(block_2011,-1)!="II" & word(block_2011,-1)!="III" & word(block_2011,-1)!="WEST" & word(block_2011,-1)!="EAST" & word(block_2011,-1)!="SOUTH" & word(block_2011,-1)!="NORTH"  
replace block_name_merge = block_name_merge_below if block_name_merge=="" & word(block_name_merge_below,-1)!="I" & word(block_name_merge_below,-1)!="II" & word(block_name_merge_below,-1)!="III" & word(block_name_merge_below,-1)!="WEST" & word(block_name_merge_below,-1)!="EAST" & word(block_name_merge_below,-1)!="SOUTH" & word(block_name_merge_below,-1)!="NORTH" & word(block_2011,-1)!="I" & word(block_2011,-1)!="II" & word(block_2011,-1)!="III" & word(block_2011,-1)!="WEST" & word(block_2011,-1)!="EAST" & word(block_2011,-1)!="SOUTH" & word(block_2011,-1)!="NORTH"  
gen replace_text = "replace block = " + q + block_name_merge + q + " if block==" + q + block_2011 + q + " & district==" + q + district + q + " & state==" + q + state + q if block_name_merge!="" & word(block_name_merge,-1)!="I" & word(block_name_merge,-1)!="II" & word(block_name_merge,-1)!="III" & word(block_2011,-1)!="I" & word(block_2011,-1)!="II" & word(block_2011,-1)!="III"
br if replace_text!=""
*/
drop if merge_bkconc11==2
duplicates drop
rename block_2011 block
replace bk_code_conc11 = . if block=="SIRPUR" & district=="ADILABAD" & state=="ANDHRA PRADESH"
merge m:m state st_code district dt_code block using "$hab3/hab_census03_for_merge.dta", nogen
order st_code state dt_code district block bk_code_pca bk_code11 bk_code_conc01 bk_code_conc11
sort  st_code state dt_code district block bk_code_pca bk_code11 bk_code_conc01 bk_code_conc11
save "$hab3/hab_census03_for_merge.dta", replace



use "$hab3/hab_census03_for_merge.dta", clear // try RGGVY block names
keep st_code state dt_code district block merge*
rename block block_rggvy
duplicates drop
merge 1:m st_code state dt_code district block_rggvy using "$hmerge/pca_2001_names_merges_all.dta", gen(merge_bkrggvy)
keep st_code state dt_code district block_rggvy bk_code_rggvy merge_bk merge_bk11 merge_bkconc01 merge_bkconc11 merge_bkrggvy
sort st_code dt_code block_rggvy
replace state = state[_n-1] if state=="" & st_code==st_code[_n-1]
replace state = state[_n+1] if state=="" & st_code==st_code[_n+1]
replace district = district[_n-1] if district=="" & dt_code==dt_code[_n-1]
replace district = district[_n+1] if district=="" & dt_code==dt_code[_n+1]
duplicates drop 
/*
keep if merge_bkrggvy<3 & merge_bk!=3 & merge_bk11!=3 & merge_bkconc01!=3 & merge_bkconc11!=3
drop if block_rggvy==""
gen block_name_merge = ""  // create field for merge name, to go back and replace when insheeting habitation census
gen q = `"""'

*Iterate 2x to correct blocks that are 1-2 letters off
gen block_pca_above = block_rggvy[_n-1] if merge_bkrggvy[_n-1]==2
gen block_pca_below = block_rggvy[_n+1] if merge_bkrggvy[_n+1]==2
strdist block_rggvy block_pca_above if merge_bkrggvy==1, gen(dist_above)
strdist block_rggvy block_pca_below if merge_bkrggvy==1, gen(dist_below)
br block_rggvy block_pca_above dist_above if dist_above<3
br block_rggvy block_pca_below dist_below if dist_below<3
replace block_name_merge = block_pca_above if merge_bkrggvy==1 & merge_bkrggvy[_n-1]==2 & dist_above<3 & dist_below>3 & state==state[_n-1] & district==district[_n-1] 
replace block_name_merge = block_pca_below if merge_bkrggvy==1 & merge_bkrggvy[_n+1]==2 & dist_below<3 & dist_above>3 & state==state[_n+1] & district==district[_n+1] 
gen replace_text = "replace block = " + q + block_name_merge + q + " if block==" + q + block_rggvy + q + " & district==" + q + district + q + " & state==" + q + state + q if block_name_merge!="" & word(block_name_merge,-1)!="I" & word(block_name_merge,-1)!="II" & word(block_name_merge,-1)!="III" & word(block_rggvy,-1)!="I" & word(block_rggvy,-1)!="II" & word(block_rggvy,-1)!="III"
br if replace_text!=""

*Iterate 1x to correct blocks that have a matching word
gen block_pca_above = block_rggvy[_n-1] if merge_bkrggvy[_n-1]==2
gen block_pca_below = block_rggvy[_n+1] if merge_bkrggvy[_n+1]==2
gen block_name_merge_above = block_pca_above if merge_bkrggvy==1 & merge_bkrggvy[_n-1]==2 & word(block_rggvy,1)==word(block_pca_above,1) & state==state[_n-1] & district==district[_n-1] 
gen block_name_merge_below = block_pca_below if merge_bkrggvy==1 & merge_bkrggvy[_n+1]==2 & word(block_rggvy,1)==word(block_pca_below,1) & state==state[_n+1] & district==district[_n+1] 
br state district block_rggvy block_pca* block_name_merge_* if block_name_merge_above!="" | block_name_merge_below!=""
replace block_name_merge = block_name_merge_above if block_name_merge=="" & word(block_name_merge_above,-1)!="I" & word(block_name_merge_above,-1)!="II" & word(block_name_merge_above,-1)!="III" & word(block_name_merge_above,-1)!="WEST" & word(block_name_merge_above,-1)!="EAST" & word(block_name_merge_above,-1)!="SOUTH" & word(block_name_merge_above,-1)!="NORTH" & word(block_rggvy,-1)!="I" & word(block_rggvy,-1)!="II" & word(block_rggvy,-1)!="III" & word(block_rggvy,-1)!="WEST" & word(block_rggvy,-1)!="EAST" & word(block_rggvy,-1)!="SOUTH" & word(block_rggvy,-1)!="NORTH"  
replace block_name_merge = block_name_merge_below if block_name_merge=="" & word(block_name_merge_below,-1)!="I" & word(block_name_merge_below,-1)!="II" & word(block_name_merge_below,-1)!="III" & word(block_name_merge_below,-1)!="WEST" & word(block_name_merge_below,-1)!="EAST" & word(block_name_merge_below,-1)!="SOUTH" & word(block_name_merge_below,-1)!="NORTH" & word(block_rggvy,-1)!="I" & word(block_rggvy,-1)!="II" & word(block_rggvy,-1)!="III" & word(block_rggvy,-1)!="WEST" & word(block_rggvy,-1)!="EAST" & word(block_rggvy,-1)!="SOUTH" & word(block_rggvy,-1)!="NORTH"  
gen replace_text = "replace block = " + q + block_name_merge + q + " if block==" + q + block_rggvy + q + " & district==" + q + district + q + " & state==" + q + state + q if block_name_merge!="" & word(block_name_merge,-1)!="I" & word(block_name_merge,-1)!="II" & word(block_name_merge,-1)!="III" & word(block_rggvy,-1)!="I" & word(block_rggvy,-1)!="II" & word(block_rggvy,-1)!="III"
br if replace_text!=""
*/
drop if merge_bkrggvy==2
duplicates drop
rename block_rggvy block
replace bk_code_rggvy = . if block=="SIRPUR" & district=="ADILABAD" & state=="ANDHRA PRADESH"
merge m:m state st_code district dt_code block using "$hab3/hab_census03_for_merge.dta", nogen
order st_code state dt_code district block bk_code_pca bk_code11 bk_code_conc01 bk_code_conc11 bk_code_rggvy
sort  st_code state dt_code district block bk_code_pca bk_code11 bk_code_conc01 bk_code_conc11 bk_code_rggvy
save "$hab3/hab_census03_for_merge.dta", replace



use "$hab3/hab_census03_for_merge.dta", clear // try RGGVY hab list block names
keep st_code state dt_code district block merge*
rename block block_lh
duplicates drop
merge 1:m st_code state dt_code district block_lh using "$hmerge/pca_2001_names_merges_all.dta", gen(merge_bklh)
keep st_code state dt_code district block_lh bk_code_lh merge_bk merge_bk11 merge_bkconc01 merge_bkconc11 merge_bkrggvy merge_bklh
sort st_code dt_code block_lh
replace state = state[_n-1] if state=="" & st_code==st_code[_n-1]
replace state = state[_n+1] if state=="" & st_code==st_code[_n+1]
replace district = district[_n-1] if district=="" & dt_code==dt_code[_n-1]
replace district = district[_n+1] if district=="" & dt_code==dt_code[_n+1]
duplicates drop 
/*
keep if merge_bklh<3 & merge_bk!=3 & merge_bk11!=3 & merge_bkconc01!=3 & merge_bkconc11!=3 & merge_bkrggvy!=3
drop if block_lh==""
gen block_name_merge = ""  // create field for merge name, to go back and replace when insheeting habitation census
gen q = `"""'

*Iterate 2x to correct blocks that are 1-2 letters off
gen block_pca_above = block_lh[_n-1] if merge_bklh[_n-1]==2
gen block_pca_below = block_lh[_n+1] if merge_bklh[_n+1]==2
strdist block_lh block_pca_above if merge_bklh==1, gen(dist_above)
strdist block_lh block_pca_below if merge_bklh==1, gen(dist_below)
br block_lh block_pca_above dist_above if dist_above<3
br block_lh block_pca_below dist_below if dist_below<3
replace block_name_merge = block_pca_above if merge_bklh==1 & merge_bklh[_n-1]==2 & dist_above<3 & dist_below>3 & state==state[_n-1] & district==district[_n-1] 
replace block_name_merge = block_pca_below if merge_bklh==1 & merge_bklh[_n+1]==2 & dist_below<3 & dist_above>3 & state==state[_n+1] & district==district[_n+1] 
gen replace_text = "replace block = " + q + block_name_merge + q + " if block==" + q + block_lh + q + " & district==" + q + district + q + " & state==" + q + state + q if block_name_merge!="" & word(block_name_merge,-1)!="I" & word(block_name_merge,-1)!="II" & word(block_name_merge,-1)!="III" & word(block_lh,-1)!="I" & word(block_lh,-1)!="II" & word(block_lh,-1)!="III"
br if replace_text!=""

*Iterate 1x to correct blocks that have a matching word
gen block_pca_above = block_lh[_n-1] if merge_bklh[_n-1]==2
gen block_pca_below = block_lh[_n+1] if merge_bklh[_n+1]==2
gen block_name_merge_above = block_pca_above if merge_bklh==1 & merge_bklh[_n-1]==2 & word(block_lh,1)==word(block_pca_above,1) & state==state[_n-1] & district==district[_n-1] 
gen block_name_merge_below = block_pca_below if merge_bklh==1 & merge_bklh[_n+1]==2 & word(block_lh,1)==word(block_pca_below,1) & state==state[_n+1] & district==district[_n+1] 
br state district block_lh block_pca* block_name_merge_* if block_name_merge_above!="" | block_name_merge_below!=""
replace block_name_merge = block_name_merge_above if block_name_merge=="" & word(block_name_merge_above,-1)!="I" & word(block_name_merge_above,-1)!="II" & word(block_name_merge_above,-1)!="III" & word(block_name_merge_above,-1)!="WEST" & word(block_name_merge_above,-1)!="EAST" & word(block_name_merge_above,-1)!="SOUTH" & word(block_name_merge_above,-1)!="NORTH" & word(block_lh,-1)!="I" & word(block_lh,-1)!="II" & word(block_lh,-1)!="III" & word(block_lh,-1)!="WEST" & word(block_lh,-1)!="EAST" & word(block_lh,-1)!="SOUTH" & word(block_lh,-1)!="NORTH"  
replace block_name_merge = block_name_merge_below if block_name_merge=="" & word(block_name_merge_below,-1)!="I" & word(block_name_merge_below,-1)!="II" & word(block_name_merge_below,-1)!="III" & word(block_name_merge_below,-1)!="WEST" & word(block_name_merge_below,-1)!="EAST" & word(block_name_merge_below,-1)!="SOUTH" & word(block_name_merge_below,-1)!="NORTH" & word(block_lh,-1)!="I" & word(block_lh,-1)!="II" & word(block_lh,-1)!="III" & word(block_lh,-1)!="WEST" & word(block_lh,-1)!="EAST" & word(block_lh,-1)!="SOUTH" & word(block_lh,-1)!="NORTH"  
gen replace_text = "replace block = " + q + block_name_merge + q + " if block==" + q + block_lh + q + " & district==" + q + district + q + " & state==" + q + state + q if block_name_merge!="" & word(block_name_merge,-1)!="I" & word(block_name_merge,-1)!="II" & word(block_name_merge,-1)!="III" & word(block_lh,-1)!="I" & word(block_lh,-1)!="II" & word(block_lh,-1)!="III"
br if replace_text!=""
*/
drop if merge_bklh==2
duplicates drop
rename block_lh block
replace bk_code_lh = . if block=="SIRPUR" & district=="ADILABAD" & state=="ANDHRA PRADESH"
merge m:m state st_code district dt_code block using "$hab3/hab_census03_for_merge.dta", nogen
order st_code state dt_code district block bk_code_pca bk_code11 bk_code_conc01 bk_code_conc11 bk_code_rggvy bk_code_lh
sort  st_code state dt_code district block bk_code_pca bk_code11 bk_code_conc01 bk_code_conc11 bk_code_rggvy bk_code_lh
save "$hab3/hab_census03_for_merge.dta", replace
}

********************************************************************************
********************************************************************************

** Step 5: Village matches, part I: Exact 2003 matches, where we know the block code
{
use "$hmerge/pca_2001_names_merges_all.dta", clear
foreach v of varlist bk_code* {
  assert `v'>=0
	replace `v'=-1 if `v'==.
}
rename village village_pca
rename block block_pca

*Find exact merges on village name, for all block code & village name combos
foreach bk of varlist bk_code_pca bk_code11 bk_code_conc01 bk_code_conc11 bk_code_rggvy bk_code_lh {
	local bk_stub = subinstr(substr("`bk'",8,99),"_","",.)
	gen h3v_id_`bk_stub' = .
	gen match_`bk_stub'_vi = ""
	foreach vi of varlist village_pca village_vd village11 village_conc01 village_conc11 village_rggvy village_lh {
		local vi_stub = subinstr(substr("`vi'",8,99),"_","",.)
		rename `vi' village
	  merge m:m st_code dt_code `bk' village using "$hab3/hab_census03_for_merge.dta", keep(1 3) keepusing(h3v_id)
	  replace h3v_id_`bk_stub' = h3v_id if h3v_id!=. & h3v_id_`bk_stub'==. & _merge==3
		replace match_`bk_stub'_vi = match_`bk_stub'_vi + " `vi_stub'" if _merge==3 
		rename village `vi'
		drop h3v_id _merge
	}
}

*Create unified village match field and merge in from Hab Census data
duplicates drop
egen h3v_id_rowmin = rowmin(h3v_id_pca h3v_id_11 h3v_id_conc01 h3v_id_conc11 h3v_id_rggvy h3v_id_lh)
egen h3v_id_rowmax = rowmax(h3v_id_pca h3v_id_11 h3v_id_conc01 h3v_id_conc11 h3v_id_rggvy h3v_id_lh)
egen h3v_id_rowmin2 = rowmin(h3v_id_pca h3v_id_11 h3v_id_conc01 h3v_id_conc11 h3v_id_rggvy)
egen h3v_id_rowmax2 = rowmax(h3v_id_pca h3v_id_11 h3v_id_conc01 h3v_id_conc11 h3v_id_rggvy)
egen h3v_id_rowmin3 = rowmin(h3v_id_pca h3v_id_11 h3v_id_conc01 h3v_id_conc11)
egen h3v_id_rowmax3 = rowmax(h3v_id_pca h3v_id_11 h3v_id_conc01 h3v_id_conc11)
egen h3v_id_rowmean = rowmean(h3v_id_pca h3v_id_11 h3v_id_conc01 h3v_id_conc11 h3v_id_rggvy h3v_id_lh)
gen h3v_id = .
replace h3v_id = h3v_id_rowmin if h3v_id==. & h3v_id_rowmin==h3v_id_rowmax & h3v_id_rowmin!=. 
replace h3v_id = h3v_id_rowmin if h3v_id==. & h3v_id_rowmin2==h3v_id_rowmax2 & h3v_id_rowmin!=. 
replace h3v_id = h3v_id_rowmin if h3v_id==. & h3v_id_rowmin3==h3v_id_rowmax3 & h3v_id_rowmin!=.
replace h3v_id = h3v_id_rowmin if h3v_id==. & h3v_id_rowmin!=. & h3v_id_rowmean-h3v_id_rowmin<h3v_id_rowmax-h3v_id_rowmean
replace h3v_id = h3v_id_rowmax if h3v_id==. & h3v_id_rowmax!=. & h3v_id_rowmean-h3v_id_rowmin>h3v_id_rowmax-h3v_id_rowmean
foreach v of varlist h3v_id_* {
	replace h3v_id = `v' if `v'!=. & h3v_id==.
}
merge m:1 h3v_id using "$hab3/hab_census03_for_merge.dta", keep(1 3) keepusing(block village totp_h3 maxp_h3 count_h3)
duplicates drop


*Remove duplicates, so a single PCA village doesn't match to multiple hab census villages
duplicates t names_id, gen(dup1)
sort names_id

  // drop the few where there are just straight duplicates of both master and using identifiers
duplicates drop names_id h3v_id, force

  // drop by population and whether certain dups have less consistent matches
gen pop_diff01 = abs(tot_p - totp_h3)
gen pop_diff11 = abs(tot_p11 - totp_h3)
drop if dup1>0 & names_id==names_id[_n-1] & ((pop_diff01!=. & pop_diff01>0 & pop_diff01[_n-1]==0) | (pop_diff11!=. & pop_diff11>0 & pop_diff11[_n-1]==0))
drop if dup1>0 & names_id==names_id[_n+1] & ((pop_diff01!=. & pop_diff01>0 & pop_diff01[_n+1]==0) | (pop_diff11!=. & pop_diff11>0 & pop_diff11[_n+1]==0))
drop if dup1>0 & names_id==names_id[_n-1] & (h3v_id>h3v_id_rowmin | h3v_id<h3v_id_rowmax) & (h3v_id[_n-1]==h3v_id_rowmin[_n-1] & h3v_id[_n-1]==h3v_id_rowmax[_n-1])
drop if dup1>0 & names_id==names_id[_n+1] & (h3v_id>h3v_id_rowmin | h3v_id<h3v_id_rowmax) & (h3v_id[_n+1]==h3v_id_rowmin[_n+1] & h3v_id[_n-1]==h3v_id_rowmax[_n+1])
drop if dup1>0 & names_id==names_id[_n-1] & h3v_id==h3v_id[_n-1] & h3v_id_rowmin[_n-1]==h3v_id_rowmax[_n-1] & h3v_id_rowmin<h3v_id_rowmax
drop if dup1>0 & names_id==names_id[_n+1] & h3v_id==h3v_id[_n+1] & h3v_id_rowmin[_n+1]==h3v_id_rowmax[_n+1] & h3v_id_rowmin<h3v_id_rowmax

  // drop if there's match discrepancies within a row
gen todrop1 = 0
replace todrop1 = 1 if dup1>0 & names_id==names_id[_n-1] & (h3v_id_rowmin2!=h3v_id_rowmin | h3v_id_rowmax2!=h3v_id_rowmax) 
replace todrop1 = 1 if dup1>0 & names_id==names_id[_n+1] & (h3v_id_rowmin2!=h3v_id_rowmin | h3v_id_rowmax2!=h3v_id_rowmax) 
egen todrop1_min = min(todrop1), by(names_id)
drop if todrop1==1 & todrop1_min==0

  // drop if the other duplicate has no match discrepancies
gen todrop2 = 0
replace todrop2 = 1 if dup1>0 & names_id==names_id[_n-1] & h3v_id[_n-1]==h3v_id_rowmin[_n-1] & h3v_id[_n-1]==h3v_id_rowmax[_n-1]
replace todrop2 = 1 if dup1>0 & names_id==names_id[_n+1] & h3v_id[_n+1]==h3v_id_rowmin[_n+1] & h3v_id[_n+1]==h3v_id_rowmax[_n+1]
egen todrop2_min = min(todrop2), by(names_id)
drop if todrop2==1 & todrop2_min==0

  // drop if only the other duplicate has a block that matches
gen todrop3 = 0
replace todrop3 = 1 if dup1>0 & names_id==names_id[_n-1] & block!=block[_n-1] & h3v_id[_n-1]==h3v_id_rowmin[_n-1] & h3v_id[_n-1]==h3v_id_rowmax[_n-1]
replace todrop3 = 1 if dup1>0 & names_id==names_id[_n+1] & h3v_id[_n+1]==h3v_id_rowmin[_n+1] & h3v_id[_n+1]==h3v_id_rowmax[_n+1]
egen todrop3_min = min(todrop3), by(names_id)
drop if todrop3==1 & todrop3_min==0

  // keep the ones where the block agrees most often
gen block_match3_count = block==block_pca
replace block_match3_count = block_match3_count + (block==block11)
replace block_match3_count = block_match3_count + (block==block_2001)
replace block_match3_count = block_match3_count + (block==block_2011)
replace block_match3_count = block_match3_count + (block==block_rggvy)
replace block_match3_count = block_match3_count + (block==block_lh)
drop if dup1>0 & names_id==names_id[_n-1] & block_match3_count<block_match3_count[_n-1]
drop if dup1>0 & names_id==names_id[_n+1] & block_match3_count<block_match3_count[_n+1]

  // for matches where block NEVER agrees -- reset hab variables to zero (this should take care of remaining dups)
replace h3v_id = . 		if _merge==3 & block_match3_count==0
replace block = "" 		if _merge==3 & block_match3_count==0
replace village = "" 	if _merge==3 & block_match3_count==0
replace totp_h3 = . 		if _merge==3 & block_match3_count==0
replace maxp_h3 = . 		if _merge==3 & block_match3_count==0
replace count_h3 = .		if _merge==3 & block_match3_count==0

duplicates t names_id if h3v_id!=., gen(dup1_check)
assert dup1_check==0 if h3v_id!=.



*Remove duplicates, so a single hab census village doesn't match to multiple PCA villages
duplicates t h3v_id, gen(dup2)
replace dup2 = . if h3v_id==.
sort h3v_id

	// pick the match with the closest 2001 population, within reason
egen min_pop_diff01 = min(pop_diff01), by(h3v_id)
egen min_pop_diff11 = min(pop_diff11), by(h3v_id)
gen dup2_keep = 0
replace dup2_keep = 1 if dup2>0 & dup2!=. & min_pop_diff01==pop_diff01 & (min_pop_diff01<=0.2*tot_p | min_pop_diff01<20)
egen dup2_keep_max1 = max(dup2_keep), by(h3v_id)

	// pick the match with the closest 2011 population, within reason
*br if dup2>0 & dup2<. & dup2_keep_max1==0
replace dup2_keep = 1 if dup2>0 & dup2!=. & dup2_keep_max1==0 & min_pop_diff11==pop_diff11 & (min_pop_diff11<=0.2*tot_p11 | min_pop_diff11<20)
egen dup2_keep_max2 = max(dup2_keep), by(h3v_id)

  // find dups where the total PCA population adds up to hab population (identifies BOTH
	// villages with identical names and non-unique PCA villages in the names master dataset)
*br if dup2>0 & dup2<. & dup2_keep_max2==0
egen sum_pop01 = sum(tot_p), by(h3v_id)
egen sum_pop11 = sum(tot_p11), by(h3v_id)
replace dup2_keep = 1 if dup2>0 & dup2!=. & dup2_keep_max2==0 & (abs(totp_h3 - sum_pop01)<20 | abs(totp_h3 - sum_pop01)<0.2*sum_pop01) 
egen dup2_keep_max3 = max(dup2_keep), by(h3v_id)
replace dup2_keep = 1 if dup2>0 & dup2!=. & dup2_keep_max3==0 & (abs(totp_h3 - sum_pop11)<20 | abs(totp_h3 - sum_pop11)<0.2*sum_pop11)
egen dup2_keep_max4 = max(dup2_keep), by(h3v_id)

  // trust the h3v_id that matched the most times
*br if dup2>0 & dup2<. & dup2_keep_max4==0
gen match3_count = h3v_id_pca!=.
replace match3_count = match3_count + (h3v_id_11!=.)
replace match3_count = match3_count + (h3v_id_conc01!=.)
replace match3_count = match3_count + (h3v_id_conc11!=.)
replace match3_count = match3_count + (h3v_id_rggvy!=.)
replace match3_count = match3_count + (h3v_id_lh!=.)
gen match_all = trim(itrim(match_pca_vi + " " + match_11_vi + " " + match_conc01_vi + " " + match_conc11_vi + " " + match_rggvy_vi + " " + match_lh_vi))
egen match3_count2 = noccur(match_all), string(" ")
egen match3_count_max = max(match3_count), by(h3v_id)
egen match3_count_min = min(match3_count), by(h3v_id)
egen match3_count2_max = max(match3_count2), by(h3v_id)
egen match3_count2_min = min(match3_count2), by(h3v_id)
replace dup2_keep = 1 if dup2>0 & dup2!=. & dup2_keep_max4==0 & match3_count2==match3_count2_max & match3_count2>match3_count2_min
replace dup2_keep = 1 if dup2>0 & dup2!=. & dup2_keep_max4==0 & match3_count==match3_count_max & match3_count>match3_count_min
egen dup2_keep_max5 = max(dup2_keep), by(h3v_id)

  // i'm content to live with these dups for now, they're less than 1% of matches
*br if dup2>0 & dup2<. & dup2_keep_max5==0
replace dup2_keep = 1 if dup2>0 & dup2!=. & dup2_keep_max5==0

  // reset h3v_id and hab census variables to blanks for the weaker of the duplicate matches
replace h3v_id = . 		if dup2>0 & dup2!=. & dup2_keep==0
replace block = "" 		if dup2>0 & dup2!=. & dup2_keep==0
replace village = "" 	if dup2>0 & dup2!=. & dup2_keep==0
replace totp_h3 = .		if dup2>0 & dup2!=. & dup2_keep==0
replace maxp_h3 = . 	if dup2>0 & dup2!=. & dup2_keep==0
replace count_h3 = .	if dup2>0 & dup2!=. & dup2_keep==0
replace pop_diff01 = .	if dup2>0 & dup2!=. & dup2_keep==0
replace pop_diff11 = .	if dup2>0 & dup2!=. & dup2_keep==0
replace sum_pop01 = .		if dup2>0 & dup2!=. & dup2_keep==0
replace sum_pop11 = .		if dup2>0 & dup2!=. & dup2_keep==0

drop dup1* todrop1* todrop2* todrop3* _merge dup2* h3v_id_row* min_pop* match_all match3_count2* match3_count_*

rename block block3
rename village village3
rename pop_diff01 pop_diff01_3
rename pop_diff11 pop_diff11_3
rename sum_pop01 sum_pop_01_3
rename sum_pop11 sum_pop_11_3

*Drop duplicates and bad matches, confirm a(n almost) 1-1 match
duplicates drop
duplicates r h3v_id
duplicates r names_id


*Split "names" dataset into matched and unmatched villages
preserve
keep if h3v_id!=.
save "$hmerge/pca_2001_names_merges_all_temp_matched3.dta", replace
restore
keep if h3v_id==.
drop h3v_id_pca-match3_count
duplicates drop  
save "$hmerge/pca_2001_names_merges_all_temp_unmatched3.dta", replace

*Split hab census dataset into matched and unmatched villages
use "$hab3/hab_census03_for_merge.dta", clear
merge 1:m h3v_id using "$hmerge/pca_2001_names_merges_all_temp_matched3.dta", keepusing(h3v_id)
duplicates drop 
assert _merge!=2
preserve
keep if _merge==3
drop _merge
save "$hab3/hab_census03_for_merge_temp_matched.dta", replace
restore
keep if _merge==1
drop _merge
save "$hab3/hab_census03_for_merge_temp_unmatched.dta", replace
}

********************************************************************************
********************************************************************************

** Step 6: Village matches, part II: Exact 2003 matches, where we don't know the block code (being conservative!)
{
*Grab villages without block matches from unmatched
use "$hab3/hab_census03_for_merge_temp_unmatched.dta", clear
keep if bk_code_pca==. & bk_code11==. & bk_code_conc01==. & bk_code_conc11==. & bk_code_rggvy==. & bk_code_lh==.
assert dt_code!=.
save "$hab3/hab_census03_for_merge_temp_unmatched_nobk.dta", replace

*Merge on just st_code, dt_code, and village hame
use "$hmerge/pca_2001_names_merges_all_temp_unmatched3.dta", clear
gen h3v_id_nobk = .
gen match_nobk_vi = ""
foreach vi of varlist village_pca village_vd village11 village_conc01 village_conc11 village_rggvy village_lh {
	local vi_stub = subinstr(substr("`vi'",8,99),"_","",.)
	rename `vi' village
  merge m:m st_code dt_code village using "$hab3/hab_census03_for_merge_temp_unmatched_nobk.dta", keep(1 3) keepusing(h3v_id)
  replace h3v_id_nobk = h3v_id if h3v_id!=. & h3v_id_nobk==. & _merge==3
	replace match_nobk_vi = match_nobk_vi + " `vi_stub'" if _merge==3 
	rename village `vi'
	drop h3v_id _merge
}
duplicates drop
rename h3v_id_nobk h3v_id 
merge m:1 h3v_id using "$hab3/hab_census03_for_merge_temp_unmatched_nobk.dta", keep(1 3) keepusing(block village totp_h3 maxp_h3 count_h3)
duplicates drop

*Remove duplicates, so a single PCA village doesn't match to multiple hab census villages
duplicates t names_id, gen(dup1)
sort names_id

  // drop the few where there are just straight duplicates of both master and using identifiers
duplicates drop names_id h3v_id, force

  // find where population exactly matches
gen pop_diff01 = abs(tot_p - totp_h3)
gen pop_diff11 = abs(tot_p11 - totp_h3)
drop if dup1>0 & names_id==names_id[_n-1] & ((pop_diff01!=. & pop_diff01>0 & pop_diff01[_n-1]==0) | (pop_diff11!=. & pop_diff11>0 & pop_diff11[_n-1]==0))
drop if dup1>0 & names_id==names_id[_n+1] & ((pop_diff01!=. & pop_diff01>0 & pop_diff01[_n+1]==0) | (pop_diff11!=. & pop_diff11>0 & pop_diff11[_n+1]==0))

egen pop_diff01_mean = mean(pop_diff01), by(block* district dt_code state st_code)
egen pop_diff11_mean = mean(pop_diff11), by(block* district dt_code state st_code)
gen pop_match = pop_diff01==0 | pop_diff11==0
egen pop_match_mean = mean(pop_match), by(block* district dt_code state st_code)
egen pop_match_count = sum(pop_match), by(block* district dt_code state st_code)
egen match_count = count(names_id), by(block* district dt_code state st_code)

 // try to match a few more block names
/*
keep block* district dt_code state st_code h3v_id pop_diff*
keep if h3v_id!=.
gen id = _n
collapse (count) id (sum) count_pop_match=pop_match (mean) pop_match pop_diff01 pop_diff11, by(block* district dt_code state st_code)
gsort -id

foreach v of varlist block_pca block11 block_2001 block_2011 block_rggvy block_lh {
  strdist block `v' if `v'!="", gen(d_`v')
}
egen d_min = rowmin(d_*)
gen block_name_merge = ""
foreach v of varlist block_pca block11 block_2001 block_2011 block_rggvy block_lh {
  replace block_name_merge = `v' if block_name_merge=="" & d_`v'==d_min
}
gen q = `"""'
gen replace_text = "replace block = " + q + block_name_merge + q + " if block==" + q + block + q + " & district==" + q + district + q + " & state==" + q + state + q if block_name_merge!="" & id>=100
br if replace_text!=""
gen replace_text2= "replace block = " + q + block_name_merge + q + " if block==" + q + block + q + " & district==" + q + district + q + " & state==" + q + state + q if block_name_merge!="" & id>=50
br if replace_text=="" & replace_text2!=""
gen replace_text3= "replace block = " + q + block_name_merge + q + " if block==" + q + block + q + " & district==" + q + district + q + " & state==" + q + state + q if block_name_merge!="" & substr(block_name_merge,1,length(block))==block
br if replace_text=="" & replace_text2=="" & replace_text3!="" & block!=block_name_merge & block!="" & block_name_merge!=""
*/

*Create flag for names matches to keep
duplicates t names_id, gen(dup2)
gen keep_flag = 0
replace keep_flag = 1 if _merge==3 & (pop_match_mean>=0.5 & match_count>=20) & dup2==0
replace h3v_id = . 		if _merge==3 & keep_flag==0
replace block = "" 		if _merge==3 & keep_flag==0
replace village = "" 	if _merge==3 & keep_flag==0
replace totp_h3 = . 	if _merge==3 & keep_flag==0
replace maxp_h3 = . 	if _merge==3 & keep_flag==0
replace count_h3 = .	if _merge==3 & keep_flag==0
foreach v of varlist pop_diff01-match_count {
  replace `v' = .	if _merge==3 & keep_flag==0
}
duplicates drop
duplicates r names_id


*Remove duplicates, so a single hab census village doesn't match to multiple PCA villages
duplicates t h3v_id, gen(dup3)
replace dup3 = . if h3v_id==.
sort h3v_id

	// pick the match with the closest 2001 population, within reason
egen min_pop_diff01 = min(pop_diff01), by(h3v_id)
egen min_pop_diff11 = min(pop_diff11), by(h3v_id)
gen dup3_keep = 0
replace dup3_keep = 1 if dup3>0 & dup3!=. & min_pop_diff01==pop_diff01 & (min_pop_diff01<=0.2*tot_p | min_pop_diff01<20)
egen dup3_keep_max1 = max(dup3_keep), by(h3v_id)

	// pick the match with the closest 2011 population, within reason
*br if dup3>0 & dup3<. & dup3_keep_max1==0
replace dup3_keep = 1 if dup3>0 & dup3!=. & dup3_keep_max1==0 & min_pop_diff11==pop_diff11 & (min_pop_diff11<=0.2*tot_p11 | min_pop_diff11<20)
egen dup3_keep_max2 = max(dup3_keep), by(h3v_id)

  // find dups where the total PCA population adds up to hab population (identifies BOTH
	// villages with identical names and non-unique PCA villages in the names master dataset)
*br if dup3>0 & dup3<. & dup3_keep_max2==0
egen sum_pop01 = sum(tot_p), by(h3v_id)
egen sum_pop11 = sum(tot_p11), by(h3v_id)
replace dup3_keep = 1 if dup3>0 & dup3!=. & dup3_keep_max2==0 & (abs(totp_h3 - sum_pop01)<20 | abs(totp_h3 - sum_pop01)<0.2*sum_pop01) 
egen dup3_keep_max3 = max(dup3_keep), by(h3v_id)
replace dup3_keep = 1 if dup3>0 & dup3!=. & dup3_keep_max3==0 & (abs(totp_h3 - sum_pop11)<20 | abs(totp_h3 - sum_pop11)<0.2*sum_pop11)
egen dup3_keep_max4 = max(dup3_keep), by(h3v_id)

  // i'm content to live with these dups for now, they're less than 1% of matches
*br if dup3>0 & dup3<. & dup3_keep_max4==0
replace dup3_keep = 1 if dup3>0 & dup3!=. & dup3_keep_max4==0

  // reset h3v_id and hab census variables to blanks for the weaker of the duplicate matches
replace h3v_id = . 		if dup3>0 & dup3!=. & dup3_keep==0
replace block = "" 		if dup3>0 & dup3!=. & dup3_keep==0
replace village = "" 	if dup3>0 & dup3!=. & dup3_keep==0
replace totp_h3 = . 		if dup3>0 & dup3!=. & dup3_keep==0
replace maxp_h3 = . 		if dup3>0 & dup3!=. & dup3_keep==0
replace count_h3 = .		if dup3>0 & dup3!=. & dup3_keep==0
replace pop_diff01 = .	if dup3>0 & dup3!=. & dup3_keep==0
replace pop_diff11 = .	if dup3>0 & dup3!=. & dup3_keep==0
replace sum_pop01 = .		if dup3>0 & dup3!=. & dup3_keep==0
replace sum_pop11 = .		if dup3>0 & dup3!=. & dup3_keep==0
gen h3v_id_nobk = h3v_id

drop dup1* dup2* dup3* keep* min_pop* pop_diff*_mean pop_match* match_count _merge

rename block block3
rename village village3
rename pop_diff01 pop_diff01_3
rename pop_diff11 pop_diff11_3
rename sum_pop01 sum_pop_01_3
rename sum_pop11 sum_pop_11_3

*Drop duplicates and bad matches, confirm a(n almost) 1-1 match
duplicates drop
duplicates r h3v_id
duplicates r names_id

*Update "names" matched and unmatched datasets
preserve
keep if h3v_id!=.
append using "$hmerge/pca_2001_names_merges_all_temp_matched3.dta"
order st_code-vdpr3 h3v_id_pca-match_lh_vi h3v_id_nobk match_nobk_vi 
save "$hmerge/pca_2001_names_merges_all_temp_matched3.dta", replace
restore
keep if h3v_id==.
drop h3v_id-h3v_id_nobk
duplicates drop
save "$hmerge/pca_2001_names_merges_all_temp_unmatched3.dta", replace


*Split hab census dataset into matched and unmatched villages
use "$hab3/hab_census03_for_merge.dta", clear
merge 1:m h3v_id using "$hmerge/pca_2001_names_merges_all_temp_matched3.dta", keepusing(h3v_id)
duplicates drop 
assert _merge!=2
preserve
keep if _merge==3
drop _merge
save "$hab3/hab_census03_for_merge_temp_matched.dta", replace
restore
keep if _merge==1
drop _merge
save "$hab3/hab_census03_for_merge_temp_unmatched.dta", replace

}
  // send these unmatched files to the server for a fuzzy (reclink) merge
  // "merge_habitation_fuzzy_2003.do", Steps 1-4
	
********************************************************************************
********************************************************************************

** Step 7: Prep 2009 Water Habitation survey for merge
{
insheet using "$hab9/BASIC_HABITATION_INFORMATION_AS_ON_1_APR_09.csv", clear comma names double
duplicates drop
rename statename state
rename districtname district
rename blockname block
rename panchayatname panchayat
rename villagename village
rename habitationname habitation
rename sccurrentpopulation scp
rename stcurrentpopulation stp
rename generalcurrentpopulation totp
keep state district block panchayat village habitation scp stp totp
tab state
replace state = subinstr(state,"&amp;","AND",.)
replace state = "JAMMU & KASHMIR" if state=="JAMMU AND KASHMIR"
replace state = "CHHATTISGARH" if state=="CHATTISGARH"
replace state = "UTTARAKHAND" if state=="UTTRANCHAL"
replace state = upper(trim(itrim(state)))
replace district = upper(trim(itrim(district)))
gen APdt_code = real(substr(district,-3,2)) if state=="ANDHRA PRADESH" & substr(district,-4,1)=="(" & substr(district,-1,1)==")"
replace district = substr(district,1,length(district)-4) if state=="ANDHRA PRADESH" & APdt_code!=.

replace district = "KURUNG KUMAI" if district=="KURUNG KUMEY" & state=="ARUNACHAL PRADESH"
*replace district = "PASCHIM CHAMPARAN" if district=="PASHCHIM CHAMPARAN" & state=="BIHAR"
*replace district = "PURAV CHAMPARAN" if district=="PURBA CHAMPARAN" & state=="BIHAR"
replace district = "BASTAR" if district=="BASTER" & state=="CHHATTISGARH"
replace district = "LAHUL & SPITI" if district=="LAHUL AND SPITI" & state=="HIMACHAL PRADESH"
replace district = "BARAMULA" if district=="BARAMULLA" & state=="JAMMU & KASHMIR"
replace district = "PUNCH" if district=="POONCH" & state=="JAMMU & KASHMIR"
*replace district = "HAZARIBAG" if district=="HAZARIBAGH" & state=="JHARKHAND"
replace district = "KODARMA" if district=="KODERMA" & state=="JHARKHAND"
replace district = "PAKAUR" if district=="PAKUR" & state=="JHARKHAND"
replace district = "PASHCHIMI SINGHBHUM" if district=="PASCHIM SINGHBHUM" & state=="JHARKHAND"
replace district = "SARAIKELA KHARSAWAN" if district=="SAREIKELA AND KHARSAWAN" & state=="JHARKHAND"
replace district = "BANGALORE" if district=="BANGLORE URBAN" & state=="KARNATAKA"
*replace district = "CHAMRAJNAGAR" if district=="CHAMARAJANAGAR" & state=="KARNATAKA"
replace district = "DAKSHINA KANNADA" if district=="DAKSHIN KANNAD" & state=="KARNATAKA"
replace district = "DAVANAGERE" if district=="DAVANGERE" & state=="KARNATAKA"
replace district = "UTTARA KANNADA" if district=="UTTAR KANNADA" & state=="KARNATAKA"
replace district = "EAST NIMAR" if district=="EAST NIMAR KHARGONE" & state=="MADHYA PRADESH"
replace district = "NARSIMHAPUR" if district=="NARSINGHPUR" & state=="MADHYA PRADESH"
replace district = "EAST NIMAR KHARGONE" if district=="EAST NIMAR" & state=="MADHYA PRADESH"
*replace district = "FATEHGARHSAHIB" if district=="FATEHGARH SAHIB" & state=="PUNJAB"
replace district = "FIROZPUR" if district=="FEROZPUR" & state=="PUNJAB"
replace district = "KANCHEEPURAM" if district=="KANCHIPURAM" & state=="TAMIL NADU"
replace district = "KANNIYAKUMARI" if district=="KANNIYAKUMARI (HQ: NAGERCOIL)" & state=="TAMIL NADU"
replace district = "THOOTHUKKUDI" if district=="THOOTHUKUDI" & state=="TAMIL NADU"
replace district = "VILUPPURAM" if district=="VILLUPURAM" & state=="TAMIL NADU"
replace district = "THIRUVALLUR" if district=="TIRUVALLUR" & state=="TAMIL NADU"
replace district = "THIRUVARUR" if district=="TIRUVARUR" & state=="TAMIL NADU"
replace district = "THE NILGIRIS" if district=="NILGIRIS (HQ: UDHAGAMANDALAM)" & state=="TAMIL NADU"
replace district = "DHALAI" if district=="DHALAI DISTRICT" & state=="TRIPURA"
replace district = "BULANDSHAHAR" if district=="BULANDSHAHR" & state=="UTTAR PRADESH"
replace district = "KUSHINAGAR" if district=="KUSHI NAGAR" & state=="UTTAR PRADESH"
replace district = "MAHRAJGANJ" if district=="MAHARAJGANJ" & state=="UTTAR PRADESH"
replace district = "SIDDHARTHNAGAR" if district=="SIDDHARTH NAGAR" & state=="UTTAR PRADESH"
replace district = "JYOTIBA PHULE NAGAR" if district=="JYOTIBA PHOOLE NAGAR" & state=="UTTAR PRADESH"
replace district = "SANT KABIR NAGAR" if district=="SANT KABEER NAGAR" & state=="UTTAR PRADESH"
replace district = "JYOTIBA PHULE NAGAR" if district=="JYOTIBA PHOOLE NAGAR" & state=="UTTAR PRADESH"
*replace district = "PAURI GARHWAL" if district=="GARHWAL" & state=="UTTARAKHAND"
replace district = "BARDDHAMAN" if district=="BARDHAMAN" & state=="WEST BENGAL"
replace district = "DARJILING" if district=="DARJEELING" & state=="WEST BENGAL"
replace district = "HAORA" if district=="HOWRAH" & state=="WEST BENGAL"
replace district = "HUGLI" if district=="HOOGHLY" & state=="WEST BENGAL"
replace district = "KOCH BIHAR" if district=="COOCHBEHAR" & state=="WEST BENGAL"
replace district = "PURULIYA" if district=="PURULIA" & state=="WEST BENGAL"
replace district = subinstr(district,"24","TWENTY FOUR",1) if state=="WEST BENGAL"
replace district = subinstr(district,"PARAGANAS","PARGANAS",1) if state=="WEST BENGAL"
replace district = "EAST NIMAR" if district=="EAST NIMAR KHARGONE" & state=="MADHYA PRADESH"
replace district = "WEST NIMAR" if district=="KHARGONE" & state=="MADHYA PRADESH"
replace district = "PASHCHIMI SINGHBHUM" if district=="SARAIKELA KHARSAWAN" & state=="JHARKHAND"
replace district = "KURUNG KUMEY" if district=="KURUNG KUMAI" & state=="ARUNACHAL PRADESH"
replace district = "CUDDAPAH" if district=="KADAPA" & state=="ANDHRA PRADESH"
replace district = "MAHBUBNAGAR" if district=="MAHABOOBNAGAR" & state=="ANDHRA PRADESH"
replace district = "BAKSA" if district=="BAKSHA" & state=="ASSAM"
replace district = "BHOJPUR" if district=="BHOJPUR(AARA)" & state=="BIHAR"
replace district = "JANJGIR - CHAMPA" if district=="JANJGIR-CHAMPA" & state=="CHHATTISGARH"
replace district = "RAJNANDGAON" if district=="RAJNANDAGON" & state=="CHHATTISGARH"
replace district = "THE DANGS" if district=="DANG" & state=="GUJARAT"
replace district = "KHUNTI" if district=="KHUTI" & state=="JHARKHAND"
replace district = "CHIKKABALLAPURA" if district=="CHIK BALLAPUR" & state=="KARNATAKA"
replace district = "RAMANAGARA" if district=="RAMANAGARAM" & state=="KARNATAKA"
replace district = "KHANDWA (EAST NIMAR)" if district=="KHANDWA" & state=="MADHYA PRADESH"
replace district = "SINGRAULI" if district=="SINGROULI" & state=="MADHYA PRADESH"
replace district = "AHMADNAGAR" if district=="AHMEDNAGAR" & state=="MAHARASHTRA"
replace district = "BID" if district=="BEED" & state=="MAHARASHTRA"
replace district = "GONDIYA" if district=="GONDIA" & state=="MAHARASHTRA"
replace district = "RAIGARH" if district=="RAIGAD" & state=="MAHARASHTRA"
replace district = "BID" if district=="BEED" & state=="MAHARASHTRA"
replace district = "SONAPUR" if district=="SONEPUR" & state=="ORISSA"
replace district = "FIROZPUR" if district=="FIROZEPUR" & state=="PUNJAB"
replace district = "SAHIBZADA AJIT SINGH NAGAR" if district=="MOHALI" & state=="PUNJAB"
replace district = "SHAHID BHAGAT SINGH NAGAR" if district=="SHAHEED BHAGAT SINGH NAGAR" & state=="PUNJAB"
replace district = "KANNIYAKUMARI" if word(district,1)=="KANNIYAKUMARI" & state=="TAMIL NADU"
replace district = "THE NILGIRIS" if word(district,1)=="NILGIRIS" & state=="TAMIL NADU"
replace district = "SHRAWASTI" if word(district,1)=="SHRAVASTI" & state=="UTTAR PRADESH"



replace block = upper(trim(itrim(block)))
replace block = subinstr(subinstr(subinstr(block,"*","",.)," -"," ",.),"- "," ",.)
replace block = subinstr(subinstr(subinstr(subinstr(block,"( ","(",.)," )",")",.),"-I"," I",.),"-1"," I",.)
replace block = subinstr(subinstr(subinstr(block,"(T)","",.),"(S.T)","",.),"(M)","",.)
replace block = subinstr(subinstr(subinstr(block,"C.D.BLOCK","",.),"SUB-DIV.","",.),"SUB-DIVISION","",.)
replace block = trim(itrim(subinstr(subinstr(subinstr(block,"(P)","",.),"CIRCLE","",.),"CD BLOCK","",.)))
gen APbk_code = real(substr(block,-3,2)) if state=="ANDHRA PRADESH" & substr(block,-4,1)=="(" & substr(block,-1,1)==")"
replace block = substr(block,1,length(block)-4) if state=="ANDHRA PRADESH" & APbk_code!=.
replace block = upper(trim(itrim(block)))

{
replace block = "BAZARHATHNOOR" if block=="BAZARHATNOOR" & district=="ADILABAD" & state=="ANDHRA PRADESH"
replace block = "GUDIHATHNOOR" if block=="GUDIHATNOOR" & district=="ADILABAD" & state=="ANDHRA PRADESH"
replace block = "INDERAVELLY" if block=="INDERVELLY" & district=="ADILABAD" & state=="ANDHRA PRADESH"
replace block = "KAGAZNAGAR" if block=="KAGHAZNAGAR" & district=="ADILABAD" & state=="ANDHRA PRADESH"
replace block = "KOUTHALA" if block=="KOUTALA" & district=="ADILABAD" & state=="ANDHRA PRADESH"
replace block = "LOKESWARAM" if block=="LOKESHWARAM" & district=="ADILABAD" & state=="ANDHRA PRADESH"
replace block = "NENNAL" if block=="NENNEL" & district=="ADILABAD" & state=="ANDHRA PRADESH"
replace block = "REBBANA" if block=="REBBENA" & district=="ADILABAD" & state=="ANDHRA PRADESH"
replace block = "TANOOR" if block=="THANOOR" & district=="ADILABAD" & state=="ANDHRA PRADESH"
replace block = "WANKDI" if block=="WANKIDI" & district=="ADILABAD" & state=="ANDHRA PRADESH"
replace block = "BUKKARAYA SAMUDRAM" if block=="BUKKARAYASAMUDRAM" & district=="ANANTAPUR" & state=="ANDHRA PRADESH"
replace block = "NAMBULIPULIKUNTA" if block=="NAMBULAPULAKUNTA" & district=="ANANTAPUR" & state=="ANDHRA PRADESH"
replace block = "OBULADEVARACHERUVU" if block=="OBULADEVARECHERUVU" & district=="ANANTAPUR" & state=="ANDHRA PRADESH"
replace block = "TANAKAL" if block=="TANAKALLU" & district=="ANANTAPUR" & state=="ANDHRA PRADESH"
replace block = "VAJRAKARUR" if block=="VIJRAKARUR" & district=="ANANTAPUR" & state=="ANDHRA PRADESH"
replace block = "BANGARUPALEM" if block=="BANGARUPALYAM" & district=="CHITTOOR" & state=="ANDHRA PRADESH"
replace block = "BUCHINAIDU KANDRIGA" if block=="BUCHINAIDU KHANDRIGA" & district=="CHITTOOR" & state=="ANDHRA PRADESH"
replace block = "PEDDA THIPPASAMUDRAM" if block=="PEDDATHIPPA SAMUDRAM" & district=="CHITTOOR" & state=="ANDHRA PRADESH"
replace block = "PENUMURU" if block=="PENUMUR" & district=="CHITTOOR" & state=="ANDHRA PRADESH"
replace block = "PILERU" if block=="PILER" & district=="CHITTOOR" & state=="ANDHRA PRADESH"
replace block = "RAMACHANDRAPURAM" if block=="RAMACHANDRA PURAM" & district=="CHITTOOR" & state=="ANDHRA PRADESH"
replace block = "SODAM" if block=="SODUM" & district=="CHITTOOR" & state=="ANDHRA PRADESH"
replace block = "TIRUPATI (RURAL)" if block=="TIRUPATHI (RURAL)" & district=="CHITTOOR" & state=="ANDHRA PRADESH"
replace block = "YADAMARRI" if block=="YADAMARI" & district=="CHITTOOR" & state=="ANDHRA PRADESH"
replace block = "CHAKRAYAPET" if block=="CHAKARAYAPET" & district=="CUDDAPAH" & state=="ANDHRA PRADESH"
replace block = "CHAPAD" if block=="CHAPADU" & district=="CUDDAPAH" & state=="ANDHRA PRADESH"
replace block = "PENDLIMARRI" if block=="PENDLIMARRY" & district=="CUDDAPAH" & state=="ANDHRA PRADESH"
replace block = "PULLAMPETA" if block=="PULLAMPET" & district=="CUDDAPAH" & state=="ANDHRA PRADESH"
replace block = "SRI AVADHUTHA KASINAYANA" if block=="SRI AVADHUTH KASINAYANA" & district=="CUDDAPAH" & state=="ANDHRA PRADESH"
replace block = "T SUNDUPALLE" if block=="T.SUNDUPALLE" & district=="CUDDAPAH" & state=="ANDHRA PRADESH"
replace block = "CHILAKALURIPET H/O.PURUSHOTHA PATNAM" if block=="CHILAKALURIPET H/O.PURUSHOTHA PATNA" & district=="GUNTUR" & state=="ANDHRA PRADESH"
replace block = "NARASARAOPET" if block=="NARASARAOPETA" & district=="GUNTUR" & state=="ANDHRA PRADESH"
replace block = "VELDURTHI" if block=="VELDURTHY" & district=="GUNTUR" & state=="ANDHRA PRADESH"
replace block = "ELKATHURTHI" if block=="ELKATHURTHY" & district=="KARIMNAGAR" & state=="ANDHRA PRADESH"
replace block = "SIRCILLA" if block=="SIRSILLA" & district=="KARIMNAGAR" & state=="ANDHRA PRADESH"
replace block = "SULTANABAD" if block=="SULTHANABAD" & district=="KARIMNAGAR" & state=="ANDHRA PRADESH"
replace block = "ASWARAOPETA" if block=="ASWARAOPET" & district=="KHAMMAM" & state=="ANDHRA PRADESH"
replace block = "BURGAMPAHAD" if block=="BURGUMPAHAD" & district=="KHAMMAM" & state=="ANDHRA PRADESH"
replace block = "DAMMAPETA" if block=="DAMMAPET" & district=="KHAMMAM" & state=="ANDHRA PRADESH"
replace block = "JULURPAD" if block=="JULURPADU" & district=="KHAMMAM" & state=="ANDHRA PRADESH"
replace block = "MANUGURU" if block=="MANUGUR" & district=="KHAMMAM" & state=="ANDHRA PRADESH"
replace block = "PENUBALLI" if block=="PENUBALLE" & district=="KHAMMAM" & state=="ANDHRA PRADESH"
replace block = "THIRUMALAYAPALEM" if block=="TIRUMALAYAPALEM" & district=="KHAMMAM" & state=="ANDHRA PRADESH"
replace block = "VELAIRPADU" if block=="VELAIRPAD" & district=="KHAMMAM" & state=="ANDHRA PRADESH"
replace block = "VISSANNAPET" if block=="VISSANNAPETA" & district=="KRISHNA" & state=="ANDHRA PRADESH"
replace block = "PATTIKONDA" if block=="PATHIKONDA" & district=="KURNOOL" & state=="ANDHRA PRADESH"
replace block = "PEDDA KADUBUR" if block=="PEDDA KADABUR" & district=="KURNOOL" & state=="ANDHRA PRADESH"
replace block = "JADCHERLA" if block=="JADCHARLA" & district=="MAHBUBNAGAR" & state=="ANDHRA PRADESH"
replace block = "MAHBUBNAGAR" if block=="MAHABUBNAGAR" & district=="MAHBUBNAGAR" & state=="ANDHRA PRADESH"
replace block = "NARVA" if block=="NARWA" & district=="MAHBUBNAGAR" & state=="ANDHRA PRADESH"
replace block = "VEEPANGANDLA" if block=="WEEPANGANDLA" & district=="MAHBUBNAGAR" & state=="ANDHRA PRADESH"
replace block = "DOULTABAD" if block=="DOULTHABAD" & district=="MEDAK" & state=="ANDHRA PRADESH"
replace block = "KONDAPUR" if block=="KONDAPOOR" & district=="MEDAK" & state=="ANDHRA PRADESH"
replace block = "MULUG" if block=="MULUGU" & district=="MEDAK" & state=="ANDHRA PRADESH"
replace block = "MUNPALLE" if block=="MUNIPALLE" & district=="MEDAK" & state=="ANDHRA PRADESH"
replace block = "YELDURTHY" if block=="VELDURTHY" & district=="MEDAK" & state=="ANDHRA PRADESH"
replace block = "DAMERACHERLA" if block=="DAMARACHERLA" & district=="NALGONDA" & state=="ANDHRA PRADESH"
replace block = "DEVARAKONDA" if block=="DEVARKONDA" & district=="NALGONDA" & state=="ANDHRA PRADESH"
replace block = "NARAYANAPUR" if block=="NARAYANPUR" & district=="NALGONDA" & state=="ANDHRA PRADESH"
replace block = "NEREDCHERLA" if block=="NEREDUCHERLA" & district=="NALGONDA" & state=="ANDHRA PRADESH"
replace block = "PEDDAVOORA" if block=="PEDDAVURA" & district=="NALGONDA" & state=="ANDHRA PRADESH"
replace block = "OJILI" if block=="OZILI" & district=="NELLORE" & state=="ANDHRA PRADESH"
replace block = "BHIKNOOR" if block=="BHIKNUR" & district=="NIZAMABAD" & state=="ANDHRA PRADESH"
replace block = "KAMMARPALLE" if block=="KAMMAR PALLE" & district=="NIZAMABAD" & state=="ANDHRA PRADESH"
replace block = "NAGAREDDIPET" if block=="NAGAREDDYPET" & district=="NIZAMABAD" & state=="ANDHRA PRADESH"
replace block = "BESTAWARIPETA" if block=="BESTAVARIPETA" & district=="PRAKASAM" & state=="ANDHRA PRADESH"
replace block = "CHIMAKURTHI" if block=="CHIMAKURTHY" & district=="PRAKASAM" & state=="ANDHRA PRADESH"
replace block = "GIDDALUR" if block=="GIDDALURU" & district=="PRAKASAM" & state=="ANDHRA PRADESH"
replace block = "GUDLURU" if block=="GUDLUR" & district=="PRAKASAM" & state=="ANDHRA PRADESH"
replace block = "PEDA ARAVEEDU" if block=="PEDDA RAVEEDU" & district=="PRAKASAM" & state=="ANDHRA PRADESH"
replace block = "QUTUBULLAPUR" if block=="QUTHBULLAPUR" & district=="RANGAREDDI" & state=="ANDHRA PRADESH"
replace block = "YELAL" if block=="YALAL" & district=="RANGAREDDI" & state=="ANDHRA PRADESH"
replace block = "KOTABOMMALI" if block=="KOTABOMMILI" & district=="SRIKAKULAM" & state=="ANDHRA PRADESH"
replace block = "MELIAPUTTI" if block=="MELIAPUTTU" & district=="SRIKAKULAM" & state=="ANDHRA PRADESH"
replace block = "RANASTALAM" if block=="RANASTHALAM" & district=="SRIKAKULAM" & state=="ANDHRA PRADESH"
replace block = "DATTIRAJERU" if block=="DATHIRAJERU" & district=="VIZIANAGARAM" & state=="ANDHRA PRADESH"
replace block = "DHARMASAGAR" if block=="DHARAMSAGAR" & district=="WARANGAL" & state=="ANDHRA PRADESH"
replace block = "PARKAL" if block=="PARAKAL" & district=="WARANGAL" & state=="ANDHRA PRADESH"
replace block = "MOGALTHUR" if block=="MOGALTUR" & district=="WEST GODAVARI" & state=="ANDHRA PRADESH"
replace block = "BORDUMSA" if block=="BORDUMUSA" & district=="CHANGLANG" & state=="ARUNACHAL PRADESH"
replace block = "DIYUN" if block=="DIYUM" & district=="CHANGLANG" & state=="ARUNACHAL PRADESH"
replace block = "KHIMIYONG" if block=="KHIMIYANG" & district=="CHANGLANG" & state=="ARUNACHAL PRADESH"
replace block = "LAJU" if block=="LAZU" & district=="TIRAP" & state=="ARUNACHAL PRADESH"
replace block = "MECHUKA" if block=="MECHUKHA" & district=="WEST SIANG" & state=="ARUNACHAL PRADESH"
replace block = "KATIGORA" if block=="KATIGORAH" & district=="CACHAR" & state=="ASSAM"
replace block = "KHOIRABARI" if block=="KHIRABARI" & district=="DARRANG" & state=="ASSAM"
replace block = "KATLICHARA" if block=="KATLICHERRA" & district=="HAILAKANDI" & state=="ASSAM"
replace block = "TITABOR" if block=="TITABAR" & district=="JORHAT" & state=="ASSAM"
replace block = "PATHARKANDI" if block=="PATHERKAND" & district=="KARIMGANJ" & state=="ASSAM"
replace block = "DIMOW" if block=="DEMOW" & district=="SIBSAGAR" & state=="ASSAM"
replace block = "BISWANATH" if block=="BISHAWNATH" & district=="SONITPUR" & state=="ASSAM"
replace block = "CHARIDUAR" if block=="CHAIDUAR" & district=="SONITPUR" & state=="ASSAM"
replace block = "NA-DUAR" if block=="NADUAR" & district=="SONITPUR" & state=="ASSAM"
replace block = "CHHORAHI" if block=="CHORAHI" & district=="BEGUSARAI" & state=="BIHAR"
replace block = "CHARPOKHARI" if block=="CHARPOKHRI" & district=="BHOJPUR" & state=="BIHAR"
replace block = "KATIYA" if block=="KATAIYA" & district=="GOPALGANJ" & state=="BIHAR"
replace block = "PACH DEURI" if block=="PACHDEORI" & district=="GOPALGANJ" & state=="BIHAR"
replace block = "GOGRI" if block=="GOGARI" & district=="KHAGARIA" & state=="BIHAR"
replace block = "BARIARPUR" if block=="BARIYARPUR" & district=="MUNGER" & state=="BIHAR"
replace block = "GOBINDPUR" if block=="GOVINDPUR" & district=="NAWADA" & state=="BIHAR"
replace block = "LAURIYA" if block=="LAURIA" & district=="PASHCHIM CHAMPARAN" & state=="BIHAR"
replace block = "BANMA ITAHRI" if block=="BANMA ITARHI" & district=="SAHARSA" & state=="BIHAR"
replace block = "GHAT KUSUMBHA" if block=="GHAT KUSMHA" & district=="SHEIKHPURA" & state=="BIHAR"
replace block = "HASANPURA" if block=="HASAN PURA" & district=="SIWAN" & state=="BIHAR"
replace block = "BHOPALPATTANAM" if block=="BHOPALPATNAM" & district=="DANTEWADA" & state=="CHHATTISGARH"
replace block = "NAGRI" if block=="NAGARI" & district=="DHAMTARI" & state=="CHHATTISGARH"
replace block = "BEMETRA" if block=="BEMETARA" & district=="DURG" & state=="CHHATTISGARH"
replace block = "DONDILUHARA" if block=="DONDILOHARA" & district=="DURG" & state=="CHHATTISGARH"
replace block = "JAIJAIPUR" if block=="JAIJEPUR" & district=="JANJGIR - CHAMPA" & state=="CHHATTISGARH"
replace block = "PATHALGAON" if block=="PATTHALGAON" & district=="JASHPUR" & state=="CHHATTISGARH"
replace block = "RAIPUR" if block=="MAINPUR" & district=="RAIPUR" & state=="CHHATTISGARH"
replace block = "NAVSARI" if block=="NAVSAI" & district=="NAVSARI" & state=="GUJARAT"
replace block = "JHANDUTA" if block=="JHANDUTTA" & district=="BILASPUR" & state=="HIMACHAL PRADESH"
replace block = "SALUNI" if block=="SALOONI" & district=="CHAMBA" & state=="HIMACHAL PRADESH"
replace block = "BARSAR" if block=="BAMSAN" & district=="HAMIRPUR" & state=="HIMACHAL PRADESH"
replace block = "POO" if block=="POOH" & district=="KINNAUR" & state=="HIMACHAL PRADESH"
replace block = "NERMAND" if block=="NIRMAND" & district=="KULLU" & state=="HIMACHAL PRADESH"
replace block = "LAHUL" if block=="LAHAUL" & district=="LAHUL & SPITI" & state=="HIMACHAL PRADESH"
replace block = "DHARMPUR" if block=="DHARAMPUR" & district=="MANDI" & state=="HIMACHAL PRADESH"
replace block = "SUNDARNAGAR" if block=="SUNDERNAGAR" & district=="MANDI" & state=="HIMACHAL PRADESH"
replace block = "SHALAI" if block=="SHILLAI" & district=="SIRMAUR" & state=="HIMACHAL PRADESH"
replace block = "CHADURA" if block=="CHADOORA" & district=="BADGAM" & state=="JAMMU & KASHMIR"
replace block = "BANDIPORE" if block=="BANDIPORA" & district=="BARAMULA" & state=="JAMMU & KASHMIR"
replace block = "BARAMULA" if block=="BARAMULLA" & district=="BARAMULA" & state=="JAMMU & KASHMIR"
replace block = "BISHNA" if block=="BISHNAH" & district=="JAMMU" & state=="JAMMU & KASHMIR"
replace block = "BASHOHLI" if block=="BASOHLI" & district=="KATHUA" & state=="JAMMU & KASHMIR"
replace block = "SHUPIYAN" if block=="SHOPIAN" & district=="PULWAMA" & state=="JAMMU & KASHMIR"
replace block = "NOWSHEHRA" if block=="NOWSHERA" & district=="RAJAURI" & state=="JAMMU & KASHMIR"
replace block = "RAJAURI" if block=="RAJOURI" & district=="RAJAURI" & state=="JAMMU & KASHMIR"
replace block = "GUMIA" if block=="GOMIA" & district=="BOKARO" & state=="JHARKHAND"
replace block = "PETERUAR" if block=="PETERWAR" & district=="BOKARO" & state=="JHARKHAND"
replace block = "GIDHAUR" if block=="GIRDHOUR" & district=="CHATRA" & state=="JHARKHAND"
replace block = "LAWALAUNG" if block=="LAWALONG" & district=="CHATRA" & state=="JHARKHAND"
replace block = "KARON" if block=="KAROWN" & district=="DEOGHAR" & state=="JHARKHAND"
replace block = "DHANBAD-CUM-KENDUADIH-CUM-JAGTA" if block=="DHANBAD-CUM-KENDUADIH-CUM-JOGTA" & district=="DHANBAD" & state=="JHARKHAND"
replace block = "RANISHWAR" if block=="RANESHWER" & district=="DUMKA" & state=="JHARKHAND"
replace block = "SUNDARPAHARI" if block=="SUNDAR PAHARI" & district=="GODDA" & state=="JHARKHAND"
replace block = "KURU" if block=="KURRU" & district=="LOHARDAGA" & state=="JHARKHAND"
replace block = "MAJHGAON" if block=="MANJHGAON" & district=="PASHCHIMI SINGHBHUM" & state=="JHARKHAND"
replace block = "BAHARAGORA" if block=="BAHRAGORA" & district=="PURBI SINGHBHUM" & state=="JHARKHAND"
replace block = "GOLMURI-CUM-JUGSALAI" if block=="GOLMURI CUM JUGSALAI" & district=="PURBI SINGHBHUM" & state=="JHARKHAND"
replace block = "UDHWA" if block=="UDHUA" & district=="SAHIBGANJ" & state=="JHARKHAND"
replace block = "BILGI" if block=="BILAGI" & district=="BAGALKOT" & state=="KARNATAKA"
replace block = "JAMKHANDI" if block=="JAMAKHANDI" & district=="BAGALKOT" & state=="KARNATAKA"
replace block = "DEVANAHALLI" if block=="DEVANHALLI" & district=="BANGALORE RURAL" & state=="KARNATAKA"
replace block = "DOD BALLAPUR" if block=="DODBALLAPUR" & district=="BANGALORE RURAL" & state=="KARNATAKA"
replace block = "HOSAKOTE" if block=="HOSKOTE" & district=="BANGALORE RURAL" & state=="KARNATAKA"
replace block = "HOMNABAD" if block=="HUMNABAD" & district=="BIDAR" & state=="KARNATAKA"
replace block = "KOLLEGAL" if block=="KOLLEGALA" & district=="CHAMARAJANAGAR" & state=="KARNATAKA"
replace block = "CHIKMAGALUR" if block=="CHICKMAGALUR" & district=="CHIKMAGALUR" & state=="KARNATAKA"
replace block = "BANTVAL" if block=="BANTWAL" & district=="DAKSHINA KANNADA" & state=="KARNATAKA"
replace block = "SULYA" if block=="SULLIA" & district=="DAKSHINA KANNADA" & state=="KARNATAKA"
replace block = "HARAPANAHALLI" if block=="HARAPPANAHALLI" & district=="DAVANAGERE" & state=="KARNATAKA"
replace block = "HARIHAR" if block=="HARIHARA" & district=="DAVANAGERE" & state=="KARNATAKA"
replace block = "MUNDARGI" if block=="MUNDARAGI" & district=="GADAG" & state=="KARNATAKA"
replace block = "NARGUND" if block=="NARAGUND" & district=="GADAG" & state=="KARNATAKA"
replace block = "SHIRHATTI" if block=="SHIRAHATTI" & district=="GADAG" & state=="KARNATAKA"
replace block = "CHITAPUR" if block=="CHITTAPUR" & district=="GULBARGA" & state=="KARNATAKA"
replace block = "JEVARGI" if block=="JEWARGI" & district=="GULBARGA" & state=="KARNATAKA"
replace block = "SHAHPUR" if block=="SHAHAPUR" & district=="GULBARGA" & state=="KARNATAKA"
replace block = "HOLE NARSIPUR" if block=="HOLENARSIPUR" & district=="HASSAN" & state=="KARNATAKA"
replace block = "SAKLESHPUR" if block=="SAKALESHPUR" & district=="HASSAN" & state=="KARNATAKA"
replace block = "HANGAL" if block=="HANAGAL" & district=="HAVERI" & state=="KARNATAKA"
replace block = "RANIBENNUR" if block=="RANEBENNUR" & district=="HAVERI" & state=="KARNATAKA"
replace block = "CHIK BALLAPUR" if block=="CHIKBALLAPUR" & district=="KOLAR" & state=="KARNATAKA"
replace block = "SRINIVASPUR" if block=="SRINIVASAPURA" & district=="KOLAR" & state=="KARNATAKA"
replace block = "GANGAWATI" if block=="GANGAVATHI" & district=="KOPPAL" & state=="KARNATAKA"
replace block = "YELBARGA" if block=="YELBURGA" & district=="KOPPAL" & state=="KARNATAKA"
replace block = "MALAVALLI" if block=="MALVALLI" & district=="MANDYA" & state=="KARNATAKA"
replace block = "TIRUMAKUDAL NARSIPUR" if block=="TIRUMAKUDAL-NARSIPUR" & district=="MYSORE" & state=="KARNATAKA"
replace block = "AMBALAPPUZHA" if block=="AMBALAPUZHA" & district=="ALAPPUZHA" & state=="KERALA"
replace block = "CHENGANNUR" if block=="CHENGANNOOR" & district=="ALAPPUZHA" & state=="KERALA"
replace block = "PARAVUR" if block=="PARAVOOR" & district=="ERNAKULAM" & state=="KERALA"
replace block = "TALIPARAMBA" if block=="THALIPARAMBU" & district=="KANNUR" & state=="KERALA"
replace block = "NILAMBUR" if block=="NILAMBOOR" & district=="MALAPPURAM" & state=="KERALA"
replace block = "CHITTUR" if block=="CHITTOOR" & district=="PALAKKAD" & state=="KERALA"
replace block = "MANNARKAD" if block=="MANNARKKAD" & district=="PALAKKAD" & state=="KERALA"
replace block = "OTTAPPALAM" if block=="OTTAPALAM" & district=="PALAKKAD" & state=="KERALA"
replace block = "KODUNGALLUR" if block=="KODUNGALLOOR" & district=="THRISSUR" & state=="KERALA"
replace block = "SULTHANBATHERY" if block=="SULTHANBETHERI" & district=="WAYANAD" & state=="KERALA"
replace block = "THIKRI" if block=="THIKARI" & district=="BARWANI" & state=="MADHYA PRADESH"
replace block = "BERASIA" if block=="BERASIA1" & district=="BHOPAL" & state=="MADHYA PRADESH"
replace block = "NOWGONG" if block=="NOWGAON" & district=="CHHATARPUR" & state=="MADHYA PRADESH"
*replace block = "JAMAI" if block=="HARAI" & district=="CHHINDWARA" & state=="MADHYA PRADESH"
replace block = "PARASIA" if block=="PARASIYA" & district=="CHHINDWARA" & state=="MADHYA PRADESH"
replace block = "TAMIA" if block=="TAMIYA" & district=="CHHINDWARA" & state=="MADHYA PRADESH"
replace block = "TONK KHURD" if block=="TONKKHURD" & district=="DEWAS" & state=="MADHYA PRADESH"
replace block = "JAWAD" if block=="JAVAD" & district=="NEEMUCH" & state=="MADHYA PRADESH"
replace block = "RAMPUR-BAGHELAN" if block=="RAMPUR BAGHELAN" & district=="SATNA" & state=="MADHYA PRADESH"
replace block = "PUSHPARAJGARH" if block=="PUSHPRAJGARH" & district=="SHAHDOL" & state=="MADHYA PRADESH"
replace block = "ANJANGAON SURJI" if block=="ANJANGAON SUR" & district=="AMRAVATI" & state=="MAHARASHTRA"
replace block = "CHANDUR RAILWAY" if block=="CHANDUR RAILW" & district=="AMRAVATI" & state=="MAHARASHTRA"
replace block = "ARJUNI MORGAON" if block=="ARJUNI MORGAO" & district=="GONDIYA" & state=="MAHARASHTRA"
replace block = "AUNDHA (NAGNATH)" if block=="AUNDHA (NAGNA)" & district=="HINGOLI" & state=="MAHARASHTRA"
replace block = "SHIRUR-ANANTPAL" if block=="SHIRUR-ANANTP" & district=="LATUR" & state=="MAHARASHTRA"
replace block = "KUHOBOTO" if block=="KUHUBOTO" & district=="DIMAPUR" & state=="NAGALAND"
replace block = "CHANGTONGYA" if block=="CHANTONGYA" & district=="MOKOKCHUNG" & state=="NAGALAND"
replace block = "GHATHASHI" if block=="GATHASHI" & district=="ZUNHEBOTO" & state=="NAGALAND"
replace block = "SURUHUTO" if block=="SURUHOTO" & district=="ZUNHEBOTO" & state=="NAGALAND"
replace block = "ATHMALLIK" if block=="ATHAMALLIK" & district=="ANUGUL" & state=="ORISSA"
replace block = "KANIHA" if block=="KARIHA" & district=="ANUGUL" & state=="ORISSA"
replace block = "KISHORENAGAR" if block=="KISHORE NAGAR" & district=="ANUGUL" & state=="ORISSA"
replace block = "BANGOMUNDA" if block=="BANGHAMUNDA" & district=="BALANGIR" & state=="ORISSA"
replace block = "BELPARA" if block=="BELPADA" & district=="BALANGIR" & state=="ORISSA"
replace block = "KHAPRAKHOL" if block=="KHAPARAKHOL" & district=="BALANGIR" & state=="ORISSA"
replace block = "SAINTALA" if block=="SAITALA" & district=="BALANGIR" & state=="ORISSA"
replace block = "TITLAGARH" if block=="TITILAGARH" & district=="BALANGIR" & state=="ORISSA"
replace block = "TUREKELA" if block=="TURIKELA" & district=="BALANGIR" & state=="ORISSA"
replace block = "BALESHWAR SADAR" if block=="BALESWAR SADAR" & district=="BALESHWAR" & state=="ORISSA"
replace block = "BALIAPAL" if block=="BALIPAL" & district=="BALESHWAR" & state=="ORISSA"
replace block = "BHOGRAI" if block=="BHOGORAI" & district=="BALESHWAR" & state=="ORISSA"
replace block = "OUPADA" if block=="OUAPADA" & district=="BALESHWAR" & state=="ORISSA"
replace block = "ATHAGAD" if block=="ATHAGARA" & district=="CUTTACK" & state=="ORISSA"
replace block = "BARANG" if block=="BARANGA" & district=="CUTTACK" & state=="ORISSA"
replace block = "SALEPUR" if block=="SALIPUR" & district=="CUTTACK" & state=="ORISSA"
replace block = "BHUBAN" if block=="BHUABAN" & district=="DHENKANAL" & state=="ORISSA"
replace block = "GANDIA" if block=="GONDIA" & district=="DHENKANAL" & state=="ORISSA"
replace block = "KAMAKSHYANAGAR" if block=="KAMAKHYANAGAR" & district=="DHENKANAL" & state=="ORISSA"
replace block = "PARAJANG" if block=="PARJANG" & district=="DHENKANAL" & state=="ORISSA"
replace block = "KASHINAGARA" if block=="KASHINAGAR" & district=="GAJAPATI" & state=="ORISSA"
replace block = "PARLAKHEMUNDI" if block=="PARALAKHEMUND" & district=="GAJAPATI" & state=="ORISSA"
replace block = "R.UDAYGIRI" if block=="R. UDAYAGIRI" & district=="GAJAPATI" & state=="ORISSA"
replace block = "ASIKA" if block=="ASKA" & district=="GANJAM" & state=="ORISSA"
replace block = "CHHATRAPUR" if block=="CHATRAPUR" & district=="GANJAM" & state=="ORISSA"
replace block = "KHALIKOTE" if block=="KHALLIKOTE" & district=="GANJAM" & state=="ORISSA"
replace block = "PATAPUR" if block=="PATRAPUR" & district=="GANJAM" & state=="ORISSA"
replace block = "PURUSOTTAMPUR" if block=="PURUSHOTTAMPUR" & district=="GANJAM" & state=="ORISSA"
replace block = "DHARAMGARH" if block=="DHARMAGARH" & district=="KALAHANDI" & state=="ORISSA"
replace block = "JAYAPATNA" if block=="JAIPATNA" & district=="KALAHANDI" & state=="ORISSA"
replace block = "KOKASARA" if block=="KOKSARA" & district=="KALAHANDI" & state=="ORISSA"
replace block = "NARALA" if block=="NARLA" & district=="KALAHANDI" & state=="ORISSA"
replace block = "DARINGBADI" if block=="DARINGIBADI" & district=="KANDHAMAL" & state=="ORISSA"
replace block = "TUMUDIBANDHA" if block=="TUMUDIBANDH" & district=="KANDHAMAL" & state=="ORISSA"
replace block = "KENDRAPARA" if block=="KENDRAPADA" & district=="KENDRAPARA" & state=="ORISSA"
replace block = "GHATGAON" if block=="GHATAGAON" & district=="KENDUJHAR" & state=="ORISSA"
replace block = "PATANA" if block=="PATNA" & district=="KENDUJHAR" & state=="ORISSA"
replace block = "BANAPUR" if block=="BANPUR" & district=="KHORDHA" & state=="ORISSA"
replace block = "JEYPUR" if block=="JEYPORE" & district=="KORAPUT" & state=="ORISSA"
replace block = "KUNDURA" if block=="KUNDRA" & district=="KORAPUT" & state=="ORISSA"
replace block = "NARAYANPATANA" if block=="NARAYANPATNA" & district=="KORAPUT" & state=="ORISSA"
replace block = "SIMILIGUDA" if block=="SEMILIGUDA" & district=="KORAPUT" & state=="ORISSA"
replace block = "BANGIRIPOSI" if block=="BANGRIPOSI" & district=="MAYURBHANJ" & state=="ORISSA"
replace block = "BETANATI" if block=="BETNOTI" & district=="MAYURBHANJ" & state=="ORISSA"
replace block = "JASHIPUR" if block=="JASIPUR" & district=="MAYURBHANJ" & state=="ORISSA"
replace block = "RARUAN" if block=="RARUA" & district=="MAYURBHANJ" & state=="ORISSA"
replace block = "RASAGOBINDAPUR" if block=="RASAGOVINDAPUR" & district=="MAYURBHANJ" & state=="ORISSA"
replace block = "TIRINGI" if block=="TIRING" & district=="MAYURBHANJ" & state=="ORISSA"
replace block = "DABUGAN" if block=="DABUGAM" & district=="NABARANGAPUR" & state=="ORISSA"
replace block = "NABARANGAPUR" if block=="NABARANGPUR" & district=="NABARANGAPUR" & state=="ORISSA"
replace block = "PAPARAHANDI" if block=="PAPADAHANDI" & district=="NABARANGAPUR" & state=="ORISSA"
replace block = "UMARKOTE" if block=="UMERKOTE" & district=="NABARANGAPUR" & state=="ORISSA"
replace block = "DASAPALLA" if block=="DASPALLA" & district=="NAYAGARH" & state=="ORISSA"
replace block = "RANAPUR" if block=="RANPUR" & district=="NAYAGARH" & state=="ORISSA"
replace block = "SINAPALI" if block=="SINAPALLI" & district=="NUAPADA" & state=="ORISSA"
replace block = "BRAHMAGIRI" if block=="BRAMHAGIRI" & district=="PURI" & state=="ORISSA"
replace block = "DELANGA" if block=="DELANG" & district=="PURI" & state=="ORISSA"
replace block = "KRUSHNA PRASAD" if block=="KRUSHNAPRASAD" & district=="PURI" & state=="ORISSA"
replace block = "NIMAPADA" if block=="NIMAPARA" & district=="PURI" & state=="ORISSA"
replace block = "KALYANASINGPUR" if block=="KALYANSINGHPUR" & district=="RAYAGADA" & state=="ORISSA"
replace block = "BINIKA" if block=="BINKA" & district=="SONAPUR" & state=="ORISSA"
replace block = "BARAGAON" if block=="BARGAON" & district=="SUNDARGARH" & state=="ORISSA"
replace block = "RAJAGANGAPUR" if block=="RAJGANGPUR" & district=="SUNDARGARH" & state=="ORISSA"
replace block = "TANGARAPALI" if block=="TANGARPALI" & district=="SUNDARGARH" & state=="ORISSA"
replace block = "KHADUR SAHIB" if block=="KHADOOR SAHIB" & district=="AMRITSAR" & state=="PUNJAB"
replace block = "TARN-TARAN" if block=="TARNTARAN" & district=="AMRITSAR" & state=="PUNJAB"
replace block = "BASSI PATHANA" if block=="BASSI PATHANAN" & district=="FATEHGARH SAHIB" & state=="PUNJAB"
replace block = "KHAMANON" if block=="KHAMANO" & district=="FATEHGARH SAHIB" & state=="PUNJAB"
replace block = "FIROZEPUR" if block=="FEROZEPUR" & district=="FIROZPUR" & state=="PUNJAB"
replace block = "GARHSHANKAR" if block=="GARH SHANKAR" & district=="HOSHIARPUR" & state=="PUNJAB"
*replace block = "HOSHIARPUR" if block=="HOSHIARPUR 1" & district=="HOSHIARPUR" & state=="PUNJAB"
replace block = "BHAGHA PURANA" if block=="BAGHAPURANA" & district=="MOGA" & state=="PUNJAB"
replace block = "NIHAL SINGHWALA" if block=="NIHAL SINGH WALA" & district=="MOGA" & state=="PUNJAB"
replace block = "GIDDARBAHA" if block=="GIDDERBAHA" & district=="MUKTSAR" & state=="PUNJAB"
*replace block = "MALERKOTLA" if block=="MALERKOTLA 1" & district=="SANGRUR" & state=="PUNJAB"
replace block = "PEESANGAN" if block=="PISANGAN" & district=="AJMER" & state=="RAJASTHAN"
replace block = "ANTAH" if block=="ANTA" & district=="BARAN" & state=="RAJASTHAN"
replace block = "ANUPGARH" if block=="ANOOPGARH" & district=="GANGANAGAR" & state=="RAJASTHAN"
replace block = "VIRATNAGAR" if block=="VIRATNAGA" & district=="JAIPUR" & state=="RAJASTHAN"
replace block = "MANOHAR THANA" if block=="MANOHARTHANA" & district=="JHALAWAR" & state=="RAJASTHAN"
replace block = "JHUNJHUNUN" if block=="JHUNJHUNU" & district=="JHUNJHUNUN" & state=="RAJASTHAN"
replace block = "NEEM-KA-THANA" if block=="NEEM KA THANA" & district=="SIKAR" & state=="RAJASTHAN"
replace block = "AVANASHI" if block=="AVINASHI" & district=="COIMBATORE" & state=="TAMIL NADU"
replace block = "NILAKKOTTAI" if block=="NILAKOTTAI" & district=="DINDIGUL" & state=="TAMIL NADU"
replace block = "TIRUKALUKUNDRAM" if block=="THIRUKALUKUNDRAM" & district=="KANCHEEPURAM" & state=="TAMIL NADU"
replace block = "KOTAGIRI" if block=="KOTHGIRI" & district=="THE NILGIRIS" & state=="TAMIL NADU"
replace block = "UDHAGAMANDALAM" if block=="UTHAGAMANDALAM" & district=="THE NILGIRIS" & state=="TAMIL NADU"
replace block = "SHENKOTTAI" if block=="SENKOTTAI" & district=="TIRUNELVELI" & state=="TAMIL NADU"
replace block = "ARAKONAM" if block=="ARAKKONAM" & district=="VELLORE" & state=="TAMIL NADU"
replace block = "KALLAKKURICHI" if block=="KALLAKURICHI" & district=="VILUPPURAM" & state=="TAMIL NADU"
replace block = "TIRUKKOYILUR" if block=="TIRUKOILUR" & district=="VILUPPURAM" & state=="TAMIL NADU"
replace block = "CHHAMANU" if block=="CHAWMANU" & district=="DHALAI" & state=="TRIPURA"
replace block = "DAMCHHARA" if block=="DAMCHERRA" & district=="NORTH TRIPURA" & state=="TRIPURA"
replace block = "JAMPUII HILLS" if block=="JAMPUIHILLS" & district=="NORTH TRIPURA" & state=="TRIPURA"
replace block = "PENCHARTHAL" if block=="PECHARTHAL" & district=="NORTH TRIPURA" & state=="TRIPURA"
replace block = "KARBUK" if block=="KARBOOK" & district=="SOUTH TRIPURA" & state=="TRIPURA"
replace block = "MATARBARI" if block=="MATABARI" & district=="SOUTH TRIPURA" & state=="TRIPURA"
replace block = "RUPAICHHARI" if block=="RUPAICHARI" & district=="SOUTH TRIPURA" & state=="TRIPURA"
replace block = "KHAIR" if block=="KAIR" & district=="ALIGARH" & state=="UTTAR PRADESH"
replace block = "HANDIA" if block=="HANDIYA" & district=="ALLAHABAD" & state=="UTTAR PRADESH"
replace block = "AZAMGARH" if block=="AZAMATGARH" & district=="AZAMGARH" & state=="UTTAR PRADESH"
replace block = "BAGHPAT" if block=="BAGPAT" & district=="BAGHPAT" & state=="UTTAR PRADESH"
replace block = "KHEKADA" if block=="KHEKARA" & district=="BAGHPAT" & state=="UTTAR PRADESH"
replace block = "BANDA" if block=="BISANDA" & district=="BANDA" & state=="UTTAR PRADESH"
replace block = "MEERGANJ" if block=="MIRGANJ" & district=="BAREILLY" & state=="UTTAR PRADESH"
replace block = "ANUPSHAHR" if block=="ANNUPSHAHR" & district=="BULANDSHAHAR" & state=="UTTAR PRADESH"
replace block = "DEBAI" if block=="DIBAI" & district=="BULANDSHAHAR" & state=="UTTAR PRADESH"
replace block = "SIANA" if block=="SYANA" & district=="BULANDSHAHAR" & state=="UTTAR PRADESH"
replace block = "CHAKARNAGAR" if block=="CHAKKARNAGAR" & district=="ETAWAH" & state=="UTTAR PRADESH"
replace block = "DADRI" if block=="DADARI" & district=="GAUTAM BUDDHA NAGAR" & state=="UTTAR PRADESH"
replace block = "BILGRAM" if block=="BILAGRAM" & district=="HARDOI" & state=="UTTAR PRADESH"
replace block = "SANDILA" if block=="SANDI" & district=="HARDOI" & state=="UTTAR PRADESH"
replace block = "MARIAHU" if block=="MARIYAHUN" & district=="JAUNPUR" & state=="UTTAR PRADESH"
replace block = "CHAIL" if block=="CHAIAL" & district=="KAUSHAMBI" & state=="UTTAR PRADESH"
replace block = "MOHAMMDI" if block=="MOHAMMADI" & district=="KHERI" & state=="UTTAR PRADESH"
replace block = "MISRIKH" if block=="MISHRIKH" & district=="SITAPUR" & state=="UTTAR PRADESH"
replace block = "DUDHI" if block=="DUDDI" & district=="SONBHADRA" & state=="UTTAR PRADESH"
replace block = "BIGHAPUR" if block=="BIGHA PUR" & district=="UNNAO" & state=="UTTAR PRADESH"
replace block = "BHIKIA SAIN" if block=="BHIKIASAIN" & district=="ALMORA" & state=="UTTARAKHAND"
replace block = "KAPKOT" if block=="KAPKOTE" & district=="BAGESHWAR" & state=="UTTARAKHAND"
replace block = "GAIR SAIN" if block=="GARSAIN" & district=="CHAMOLI" & state=="UTTARAKHAND"
replace block = "THALI SAIN" if block=="THALISAIN" & district=="GARHWAL" & state=="UTTARAKHAND"
replace block = "MUNSIARI" if block=="MUNSYARI" & district=="PITHORAGARH" & state=="UTTARAKHAND"
replace block = "DEVPRAYAG" if block=="DEVPARYAG" & district=="TEHRI GARHWAL" & state=="UTTARAKHAND"
replace block = "NARENDRANAGAR" if block=="NARENDRA NAGAR" & district=="TEHRI GARHWAL" & state=="UTTARAKHAND"
replace block = "PRATAPNAGAR" if block=="PARTAPNAGAR" & district=="TEHRI GARHWAL" & state=="UTTARAKHAND"
replace block = "PURAULA" if block=="PUROLA" & district=="UTTARKASHI" & state=="UTTARAKHAND"
replace block = "GANGAJALGHATI" if block=="GANGAJAL GHATI" & district=="BANKURA" & state=="WEST BENGAL"
replace block = "JAMALPUR" if block=="JAMAL PUR" & district=="BARDDHAMAN" & state=="WEST BENGAL"
replace block = "PANDABESWAR" if block=="PANDAVESWAR" & district=="BARDDHAMAN" & state=="WEST BENGAL"
replace block = "BOLPUR SRINIKETAN" if block=="BOLPUR-SRINIKETAN" & district=="BIRBHUM" & state=="WEST BENGAL"
replace block = "HILLI" if block=="HILI" & district=="DAKSHIN DINAJPUR" & state=="WEST BENGAL"
replace block = "KUSHMUNDI" if block=="KUSHMANDI" & district=="DAKSHIN DINAJPUR" & state=="WEST BENGAL"
replace block = "DARJEELING PULBAZAR" if block=="DARJEELING-PULBAZAR" & district=="DARJILING" & state=="WEST BENGAL"
replace block = "JOREBUNGLOW SUKIAPOKHRI" if block=="JORE BUNGLOW-SUKIAPOKHRI" & district=="DARJILING" & state=="WEST BENGAL"
replace block = "NAXALBARI" if block=="NAXAL BARI" & district=="DARJILING" & state=="WEST BENGAL"
replace block = "BALLY JAGACHHA" if block=="BALLY-JAGACHA" & district=="HAORA" & state=="WEST BENGAL"
replace block = "BAMANGOLA" if block=="BAMONGOLA" & district=="MALDAH" & state=="WEST BENGAL"
replace block = "MALDAH (OLD)" if block=="MALDA (OLD)" & district=="MALDAH" & state=="WEST BENGAL"
replace block = "JAMBONI" if block=="JAMBANI" & district=="MEDINIPUR" & state=="WEST BENGAL"
replace block = "NANDA KUMAR" if block=="NANDAKUMAR" & district=="MEDINIPUR" & state=="WEST BENGAL"
*replace block = "NANDIGRAM III" if block=="NANDIGRAM IIII" & district=="MEDINIPUR" & state=="WEST BENGAL"
replace block = "MURSHIDABAD JIAGANJ" if block=="MURSHIDABAD-JIAGUNJ" & district=="MURSHIDABAD" & state=="WEST BENGAL"
replace block = "SAMSERGANJ" if block=="SHAMSHERGANJ" & district=="MURSHIDABAD" & state=="WEST BENGAL"
replace block = "PATHARPRATIMA" if block=="PATHAR PRATIMA" & district=="SOUTH TWENTY FOUR PARGANAS" & state=="WEST BENGAL"
replace block = "SONARPUR" if block=="SONAR PUR" & district=="SOUTH TWENTY FOUR PARGANAS" & state=="WEST BENGAL"
replace block = "NERADIGONDA" if block=="NEERADIGONDA" & district=="ADILABAD" & state=="ANDHRA PRADESH"
replace block = "PICHATUR" if block=="PITCHATUR" & district=="CHITTOOR" & state=="ANDHRA PRADESH"
replace block = "BALANGIR" if block=="BOLANGIR" & district=="BALANGIR" & state=="ORISSA"
replace block = "VISHNUPUR" if block=="BISHNUPUR" & district=="BANKURA" & state=="WEST BENGAL"
replace block = "SANTHIPURAM" if block=="SANTHIPURAM H/O ARINUTHALAPALLE" & district=="CHITTOOR" & state=="ANDHRA PRADESH"
replace block = "SAVALYAPURAM H/O KANAMARLAPUDI" if block=="SAVALYAPURAM KANUMARLAPUDI" & district=="GUNTUR" & state=="ANDHRA PRADESH"
replace block = "MALHARRAO" if block=="MALHARRAO MANDAL" & district=="KARIMNAGAR" & state=="ANDHRA PRADESH"
replace block = "MUTHARAM (MAHADEVPUR)" if block=="MUTHARAM (A)" & district=="KARIMNAGAR" & state=="ANDHRA PRADESH"
replace block = "BUCHIREDDIPALEM" if block=="BUCHIREDDIPALEM H/O VAVVERU" & district=="NELLORE" & state=="ANDHRA PRADESH"
replace block = "GOLAGHAT" if block=="GOLAGHAT CENTRAL" & district=="GOLAGHAT" & state=="ASSAM"
replace block = "DHAKUAKHANA (PART I)" if block=="DHAKUAKHANA" & district=="LAKHIMPUR" & state=="ASSAM"
replace block = "GAYA TOWN" if block=="GAYA" & district=="GAYA" & state=="BIHAR"
replace block = "SONBHADRA BANSHI SURYAPUR" if block=="SONBHADRA BANSHI" & district=="JEHANABAD" & state=="BIHAR"
replace block = "PATNA RURAL" if block=="PATNA RURAL (A) PATNA RURAL (B)" & district=="PATNA" & state=="BIHAR"
replace block = "AMBAGARH" if block=="AMBAGARH CHOWKI" & district=="RAJNANDGAON" & state=="CHHATTISGARH"
replace block = "DADRI" if block=="DADRI I (PART)" & district=="BHIWANI" & state=="HARYANA"
replace block = "GHARAUNDA" if block=="GHARAUNDA (PART)" & district=="KARNAL" & state=="HARYANA"
replace block = "JUBBAL" if block=="JUBBAL KOTKHAI" & district=="SHIMLA" & state=="HIMACHAL PRADESH"
replace block = "BHALESSA (GANDOH)" if block=="BHALESSA" & district=="DODA" & state=="JAMMU & KASHMIR"
replace block = "GOOL GULAB GARH" if block=="GOOL" & district=="UDHAMPUR" & state=="JAMMU & KASHMIR"
replace block = "THIRUVANANTHAPURAM" if block=="THIRUVANANTHAPURAM RURAL" & district=="THIRUVANANTHAPURAM" & state=="KERALA"
replace block = "DHAMANGAON RAILWAY" if block=="DHAMANGAON RA" & district=="AMRAVATI" & state=="MAHARASHTRA"
replace block = "DESAIGANJ (VADASA)" if block=="DESAIGANJ (VA)" & district=="GADCHIROLI" & state=="MAHARASHTRA"
replace block = "EDLABAD (MUKTAINAGAR)" if block=="EDLABAD (MUKT)" & district=="JALGAON" & state=="MAHARASHTRA"
replace block = "NAIGAON (KHAIRGAON)" if block=="NAIGAON (KHAI)" & district=="NANDED" & state=="MAHARASHTRA"
replace block = "THINGSULTHLIAH (PART)" if block=="THINGSULTHLIAH" & district=="AIZAWL" & state=="MIZORAM"
replace block = "TLANGNUAM (PART)" if block=="TLANGNUAM" & district=="AIZAWL" & state=="MIZORAM"
replace block = "EAST LUNGDAR (PART)" if block=="EAST LUNGDAR" & district=="SERCHHIP" & state=="MIZORAM"
replace block = "KOHIMA SADAR" if block=="KOHIMA" & district=="KOHIMA" & state=="NAGALAND"
replace block = "MON SADAR" if block=="MON" & district=="MON" & state=="NAGALAND"
replace block = "PHEK SADAR" if block=="PHEK" & district=="PHEK" & state=="NAGALAND"
replace block = "KIPHIRE SADAR" if block=="KIPHIRE" & district=="TUENSANG" & state=="NAGALAND"
replace block = "WOKHA SADAR" if block=="WOKHA" & district=="WOKHA" & state=="NAGALAND"
replace block = "ZUNHEBOTO SADAR" if block=="ZUNHEBOTO" & district=="ZUNHEBOTO" & state=="NAGALAND"
replace block = "TALCHER SADAR" if block=="TALCHER" & district=="ANUGUL" & state=="ORISSA"
replace block = "BANKI" if block=="BANKI DOMOPARA" & district=="CUTTACK" & state=="ORISSA"
replace block = "TANGI" if block=="TANGI AND CHOUDWAR" & district=="CUTTACK" & state=="ORISSA"
replace block = "BARIPADA SADAR" if block=="BARIPADA" & district=="MAYURBHANJ" & state=="ORISSA"
replace block = "RAMPURA PHUL" if block=="RAMPURA" & district=="BATHINDA" & state=="PUNJAB"
replace block = "DUDU (HQ. MAUZAMABAD)" if block=="DUDU" & district=="JAIPUR" & state=="RAJASTHAN"
replace block = "JAMWA RAMGARH" if block=="JAMWA RAM" & district=="JAIPUR" & state=="RAJASTHAN"
replace block = "MARWAR JUNCTION" if block=="MARWAR JU" & district=="PALI" & state=="RAJASTHAN"
replace block = "MAWANA" if block=="MAWANA KALA" & district=="MEERUT" & state=="UTTAR PRADESH"
replace block = "MATHABHANGA II" if block=="MATHA BHANGA II" & district=="KOCH BIHAR" & state=="WEST BENGAL"
replace block = "HARISCHANDRAPUR I" if block=="HARISHCHANDRAPUR I" & district=="MALDAH" & state=="WEST BENGAL"
replace block = "HARISCHANDRAPUR II" if block=="HARISHCHANDRAPUR II" & district=="MALDAH" & state=="WEST BENGAL"
replace block = "BHAGAWANPUR I" if block=="BHAGABANPUR I" & district=="MEDINIPUR" & state=="WEST BENGAL"
replace block = "BHAGAWANPUR II" if block=="BHAGABANPUR II" & district=="MEDINIPUR" & state=="WEST BENGAL"
replace block = "GOPIBALLAVPUR II" if block=="GOPIBALLAV PUR II" & district=="MEDINIPUR" & state=="WEST BENGAL"
replace block = "POTASHPUR I" if block=="PATASHPUR I" & district=="MEDINIPUR" & state=="WEST BENGAL"
replace block = "POTASHPUR II" if block=="PATASHPUR II" & district=="MEDINIPUR" & state=="WEST BENGAL"
replace block = "BHAGAWANGOLA I" if block=="BHAGOBANGOLA I" & district=="MURSHIDABAD" & state=="WEST BENGAL"
replace block = "RAGHUNATHPUR I" if block=="RAGHUNATH PUR I" & district=="PURULIYA" & state=="WEST BENGAL"
replace block = "CHINNAMANDEM" if block=="CHINNAMUDIAM" & district=="CUDDAPAH" & state=="ANDHRA PRADESH"
replace block = "BISHAMAKATAK" if block=="BISSAMCUTTACK" & district=="RAYAGADA" & state=="ORISSA"
replace block = "SERAIKELA" if block=="SERAIKELLA" & district=="PASHCHIMI SINGHBHUM" & state=="JHARKHAND"
replace block = "HARSUD" if block=="HARSOOD" & district=="EAST NIMAR" & state=="MADHYA PRADESH"
replace block = "KHANSAHIB" if block=="KHAN-SAHIB" & district=="BADGAM" & state=="JAMMU & KASHMIR"
replace block = "KHALSI" if block=="KHALTSI" & district=="LEH (LADAKH)" & state=="JAMMU & KASHMIR"
replace block = "SANKU" if block=="SANKOO" & district=="KARGIL" & state=="JAMMU & KASHMIR"
replace block = "ANI" if block=="ANNI" & district=="KULLU" & state=="HIMACHAL PRADESH"
*replace block = "HOSHIARPUR" if block=="HOSHIARPUR 2" & district=="HOSHIARPUR" & state=="PUNJAB"
replace block = "CHINIYALISAUR" if block=="CHINYALISAUR" & district=="UTTARKASHI" & state=="UTTARAKHAND"
replace block = "JAKHNIDHAR" if block=="JAKHANIDHAR" & district=="TEHRI GARHWAL" & state=="UTTARAKHAND"
replace block = "GARUD" if block=="GARUR" & district=="BAGESHWAR" & state=="UTTARAKHAND"
replace block = "CHAUKHUTIYA" if block=="CHAUKHUTIA" & district=="ALMORA" & state=="UTTARAKHAND"
replace block = "BAJPUR" if block=="BAZPUR" & district=="UDHAM SINGH NAGAR" & state=="UTTARAKHAND"
replace block = "MATENHAIL" if block=="MATANHAIL" & district=="JHAJJAR" & state=="HARYANA"
replace block = "MUNGIAKUMI" if block=="MUNGIAKAMI" & district=="WEST TRIPURA" & state=="TRIPURA"
replace block = "MORANGI" if block=="MORONGI" & district=="GOLAGHAT" & state=="ASSAM"
replace block = "DERABISH" if block=="DERABIS" & district=="KENDRAPARA" & state=="ORISSA"
replace block = "ASTARANGA" if block=="ASTARANG" & district=="PURI" & state=="ORISSA"
replace block = "JAGANNATH PRASAD" if block=="JAGANNATHPRASAD" & district=="GANJAM" & state=="ORISSA"
replace block = "POLASARA" if block=="POLOSARA" & district=="GANJAM" & state=="ORISSA"
replace block = "SERAGAD" if block=="SHERAGADA" & district=="GANJAM" & state=="ORISSA"
replace block = "BATOULI" if block=="BATAULI" & district=="SURGUJA" & state=="CHHATTISGARH"
replace block = "OUDGI" if block=="ODGI" & district=="SURGUJA" & state=="CHHATTISGARH"
replace block = "UDAYPUR" if block=="UDAIPUR" & district=="SURGUJA" & state=="CHHATTISGARH"
replace block = "FARSABAHAR" if block=="PHARSABAHAR" & district=="JASHPUR" & state=="CHHATTISGARH"
replace block = "PATHARIYA" if block=="PATHARIA" & district=="BILASPUR" & state=="CHHATTISGARH"
replace block = "BAGBAHRA" if block=="BAGBAHARA" & district=="MAHASAMUND" & state=="CHHATTISGARH"
replace block = "DURGKONDAL" if block=="DURGUCONDAL" & district=="KANKER" & state=="CHHATTISGARH"
replace block = "BADE RAJPUR" if block=="BADERAJPUR" & district=="BASTAR" & state=="CHHATTISGARH"
replace block = "BAKAVAND" if block=="BAKABAND" & district=="BASTAR" & state=="CHHATTISGARH"
replace block = "GIDAM" if block=="GEEDAM" & district=="DANTEWADA" & state=="CHHATTISGARH"
replace block = "KUAKONDA" if block=="KUWAKONDA" & district=="DANTEWADA" & state=="CHHATTISGARH"
replace block = "MALTHON" if block=="MALTHONE" & district=="SAGAR" & state=="MADHYA PRADESH"
replace block = "ATHNER" if block=="ATHNAIR" & district=="BETUL" & state=="MADHYA PRADESH"
replace block = "GHODA DONGRI" if block=="GHORADONGRI" & district=="BETUL" & state=="MADHYA PRADESH"
replace block = "MAJHOLI" if block=="MAJHAULI" & district=="JABALPUR" & state=="MADHYA PRADESH"
replace block = "GHUGHARI" if block=="GHUGHRI" & district=="MANDLA" & state=="MADHYA PRADESH"
replace block = "MOHKHED" if block=="MOHKHEDA" & district=="CHHINDWARA" & state=="MADHYA PRADESH"
replace block = "ELGAID" if block=="ELEGAID" & district=="KARIMNAGAR" & state=="ANDHRA PRADESH"
replace block = "THANDRAMPET" if block=="THANDARAMPET" & district=="TIRUVANNAMALAI" & state=="TAMIL NADU"
replace block = "GUNNOR" if block=="GUNOUR" & district=="PANNA" & state=="MADHYA PRADESH"
replace block = "BHUBANESWAR (M.CORP.)" if block=="BHUBANESWAR" & district=="KHORDHA" & state=="ORISSA"
replace block = "JAMAI" if block=="JAMAI (JUNNARDEO)" & district=="CHHINDWARA" & state=="MADHYA PRADESH"
replace block = "RAJWARA" if block=="RAJWAR" & district=="KUPWARA" & state=="JAMMU & KASHMIR"
replace block = "TANGDHAR" if block=="TANGDAR" & district=="KUPWARA" & state=="JAMMU & KASHMIR"
replace block = "TEETWAL" if block=="TEETHWAL" & district=="KUPWARA" & state=="JAMMU & KASHMIR"
replace block = "RUHAMA" if block=="ROHAMA" & district=="BARAMULA" & state=="JAMMU & KASHMIR"
replace block = "WAGURA" if block=="WAGOORA" & district=="BARAMULA" & state=="JAMMU & KASHMIR"
replace block = "KAKA PORA" if block=="KAKAPORA" & district=="PULWAMA" & state=="JAMMU & KASHMIR"
replace block = "KOVERI PORA" if block=="KOVIRIPORA" & district=="ANANTNAG" & state=="JAMMU & KASHMIR"
replace block = "QUIMOH" if block=="QAIMOH" & district=="ANANTNAG" & state=="JAMMU & KASHMIR"
replace block = "KHAROO" if block=="KHARU" & district=="LEH (LADAKH)" & state=="JAMMU & KASHMIR"
replace block = "SHAKAR CHIKTAN" if block=="SHAKAR-CHIKTAN" & district=="KARGIL" & state=="JAMMU & KASHMIR"
replace block = "SHARGOL" if block=="SHARGOLE" & district=="KARGIL" & state=="JAMMU & KASHMIR"
replace block = "BHAGWA" if block=="BHAGWAH" & district=="DODA" & state=="JAMMU & KASHMIR"
replace block = "RAMSU" if block=="RAMSOO" & district=="DODA" & state=="JAMMU & KASHMIR"
replace block = "PANCHARI" if block=="PANCHRAI" & district=="UDHAMPUR" & state=="JAMMU & KASHMIR"
replace block = "R.S. PURA" if block=="R.S.PURA" & district=="JAMMU" & state=="JAMMU & KASHMIR"
replace block = "LOHAI MALHAR" if block=="LOHAI-MALHAR" & district=="KATHUA" & state=="JAMMU & KASHMIR"
replace block = "DERA" if block=="DEHRA" & district=="KANGRA" & state=="HIMACHAL PRADESH"
replace block = "LAMBAGRAON" if block=="LAMBAGAON" & district=="KANGRA" & state=="HIMACHAL PRADESH"
replace block = "NAGAR" if block=="NAGGAR" & district=="KULLU" & state=="HIMACHAL PRADESH"
replace block = "BIJHRI" if block=="BIJHARI" & district=="HAMIRPUR" & state=="HIMACHAL PRADESH"
replace block = "DHARMPUR" if block=="DHARAMPUR" & district=="SOLAN" & state=="HIMACHAL PRADESH"
replace block = "CHAUHRA" if block=="CHAUHARA" & district=="SHIMLA" & state=="HIMACHAL PRADESH"
replace block = "CHAUPAL" if block=="CHOPAL" & district=="SHIMLA" & state=="HIMACHAL PRADESH"
replace block = "DINANAGAR" if block=="DINA NAGAR" & district=="GURDASPUR" & state=="PUNJAB"
replace block = "FATEHGARH CHURIAN" if block=="FATEH GARH CHURIAN" & district=="GURDASPUR" & state=="PUNJAB"
replace block = "CHOLA SAHIB" if block=="CHOHLA SAHIB" & district=="AMRITSAR" & state=="PUNJAB"
replace block = "TARSIKA" if block=="TARSIKKA" & district=="AMRITSAR" & state=="PUNJAB"
replace block = "HAZIPUR" if block=="HAJIPUR" & district=="HOSHIARPUR" & state=="PUNJAB"
replace block = "DHARMKOT" if block=="DHARAMKOT" & district=="MOGA" & state=="PUNJAB"
replace block = "GURU HAR SAHAI" if block=="GURUHAR SAHAI" & district=="FIROZPUR" & state=="PUNJAB"
replace block = "KOT KAPURA" if block=="KOTKAPURA" & district=="FARIDKOT" & state=="PUNJAB"
replace block = "BHUNERHERI" if block=="BHUNNERHERI" & district=="PATIALA" & state=="PUNJAB"
replace block = "GHANAUR" if block=="GHANOUR" & district=="PATIALA" & state=="PUNJAB"
replace block = "SANAUR" if block=="SANOUR" & district=="PATIALA" & state=="PUNJAB"
replace block = "DASHOLI" if block=="DASOLI" & district=="CHAMOLI" & state=="UTTARAKHAND"
replace block = "DEWAL" if block=="DEVAL" & district=="CHAMOLI" & state=="UTTARAKHAND"
replace block = "GHAT" if block=="GHAAT" & district=="CHAMOLI" & state=="UTTARAKHAND"
replace block = "AUGUST MUNI" if block=="AUGUSTMUNI" & district=="RUDRAPRAYAG" & state=="UTTARAKHAND"
replace block = "BIROKHAL" if block=="BIRONKHAL" & district=="GARHWAL" & state=="UTTARAKHAND"
replace block = "DOGADDA" if block=="DUGADDA" & district=="GARHWAL" & state=="UTTARAKHAND"
replace block = "NAUNIDANDA" if block=="NAINIDANDA" & district=="GARHWAL" & state=="UTTARAKHAND"
replace block = "KANALI CHHINA" if block=="KANALICHHINA" & district=="PITHORAGARH" & state=="UTTARAKHAND"
replace block = "BHAISIYA CHHANA" if block=="BHAISIACHHANA" & district=="ALMORA" & state=="UTTARAKHAND"
replace block = "HAWAL BAGH" if block=="HAWALBAG" & district=="ALMORA" & state=="UTTARAKHAND"
replace block = "LAMGARHA" if block=="LAMGARA" & district=="ALMORA" & state=="UTTARAKHAND"
replace block = "OKHAL KANDA" if block=="OKHALKANDA" & district=="NAINITAL" & state=="UTTARAKHAND"
replace block = "PILLU KHERA" if block=="PILLUKHERA" & district=="JIND" & state=="HARYANA"
replace block = "SAHLAWAS" if block=="SALHAWAS" & district=="JHAJJAR" & state=="HARYANA"
replace block = "NIMRANA" if block=="NEEMRANA" & district=="ALWAR" & state=="RAJASTHAN"
replace block = "UMREN" if block=="UMRAIN" & district=="ALWAR" & state=="RAJASTHAN"
replace block = "GOVINDGARH" if block=="GOVINDGAR" & district=="JAIPUR" & state=="RAJASTHAN"
replace block = "DHOND" if block=="DHOD" & district=="SIKAR" & state=="RAJASTHAN"
replace block = "BARGAON" if block=="BADGAON" & district=="UDAIPUR" & state=="RAJASTHAN"
replace block = "BHOPALSAGAR" if block=="BHUPALSAGAR" & district=="CHITTAURGARH" & state=="RAJASTHAN"
replace block = "DAG" if block=="DUG" & district=="JHALAWAR" & state=="RAJASTHAN"
replace block = "NEHTAUR" if block=="NETAUR" & district=="BIJNOR" & state=="UTTAR PRADESH"
replace block = "ASMAULI" if block=="ASMOLI" & district=="MORADABAD" & state=="UTTAR PRADESH"
replace block = "BANIYAKHERA" if block=="BANIAKHERA" & district=="MORADABAD" & state=="UTTAR PRADESH"
replace block = "BHAGATPUR TANDA" if block=="BHARATPUR TANDA" & district=="MORADABAD" & state=="UTTAR PRADESH"
replace block = "DINGARPUR" if block=="DINGPUR" & district=="MORADABAD" & state=="UTTAR PRADESH"
replace block = "PANWASA" if block=="PAWASA" & district=="MORADABAD" & state=="UTTAR PRADESH"
replace block = "NOHJHIL" if block=="NAUHJHIL" & district=="MATHURA" & state=="UTTAR PRADESH"
replace block = "AMANPUR" if block=="AMAPUR" & district=="ETAH" & state=="UTTAR PRADESH"
replace block = "SIDHPURA" if block=="SIRHPURA" & district=="ETAH" & state=="UTTAR PRADESH"
replace block = "BHUTA" if block=="BHUNTA" & district=="BAREILLY" & state=="UTTAR PRADESH"
replace block = "BITHRICHAINPUR" if block=="BITHIRI CHAINPUR" & district=="BAREILLY" & state=="UTTAR PRADESH"
replace block = "RAMNAGAR" if block=="RAM NAGAR" & district=="BAREILLY" & state=="UTTAR PRADESH"
replace block = "AMARIYA" if block=="AMARIA" & district=="PILIBHIT" & state=="UTTAR PRADESH"
replace block = "BHAWAL KHERA" if block=="BHWALKHERA" & district=="SHAHJAHANPUR" & state=="UTTAR PRADESH"
replace block = "MITAULI" if block=="MITOLI" & district=="KHERI" & state=="UTTAR PRADESH"
replace block = "BEHADAR" if block=="BEHDAR" & district=="HARDOI" & state=="UTTAR PRADESH"
replace block = "HARIYAWAN" if block=="HARIYAWA" & district=="HARDOI" & state=="UTTAR PRADESH"
replace block = "KACHHAUNA" if block=="KACHAUNA" & district=="HARDOI" & state=="UTTAR PRADESH"
replace block = "MADHOGANJ" if block=="MADHOVGANJ" & district=="HARDOI" & state=="UTTAR PRADESH"
replace block = "MALLAWAN" if block=="MALLAWA" & district=="HARDOI" & state=="UTTAR PRADESH"
replace block = "TONDARPUR" if block=="TONDERPUR" & district=="HARDOI" & state=="UTTAR PRADESH"
replace block = "AURAS" if block=="AURAAS" & district=="UNNAO" & state=="UTTAR PRADESH"
replace block = "GANJ MORADABAD" if block=="GANJMORADABAD" & district=="UNNAO" & state=="UTTAR PRADESH"
replace block = "MIANGANJ" if block=="MIAGANJ" & district=="UNNAO" & state=="UTTAR PRADESH"
replace block = "SAROJANINAGAR" if block=="SAROJNI NAGAR" & district=="LUCKNOW" & state=="UTTAR PRADESH"
replace block = "KHIRON" if block=="KHIRO" & district=="RAE BARELI" & state=="UTTAR PRADESH"
replace block = "ROHNIYA" if block=="ROHINIYAN" & district=="RAE BARELI" & state=="UTTAR PRADESH"
replace block = "BARHPURA" if block=="BARAHPUR" & district=="FARRUKHABAD" & state=="UTTAR PRADESH"
replace block = "AJIT MAL" if block=="AJITMAL" & district=="AURAIYA" & state=="UTTAR PRADESH"
replace block = "ERWA KATRA" if block=="EKHA KATRA" & district=="AURAIYA" & state=="UTTAR PRADESH"
replace block = "SARBANKHERA" if block=="SARVANKHEDA" & district=="KANPUR DEHAT" & state=="UTTAR PRADESH"
replace block = "CHAUBEPUR" if block=="CHAUBEYPUR" & district=="KANPUR NAGAR" & state=="UTTAR PRADESH"
replace block = "DAKOR" if block=="DAKORE" & district=="JALAUN" & state=="UTTAR PRADESH"
replace block = "KUTHAUND" if block=="KUTHOND" & district=="JALAUN" & state=="UTTAR PRADESH"
replace block = "MAHEVA" if block=="MAHEWA" & district=="JALAUN" & state=="UTTAR PRADESH"
replace block = "GURSARAI" if block=="GURSRAI" & district=="JHANSI" & state=="UTTAR PRADESH"
replace block = "MAHRAUNI" if block=="MAHRAUNA" & district=="LALITPUR" & state=="UTTAR PRADESH"
replace block = "MUSKARA" if block=="MUSKRA" & district=="HAMIRPUR" & state=="UTTAR PRADESH"
replace block = "JAITPUR" if block=="JYOTPUR" & district=="MAHOBA" & state=="UTTAR PRADESH"
replace block = "TELYANI" if block=="TELIYANI" & district=="FATEHPUR" & state=="UTTAR PRADESH"
replace block = "LAKSHAMANPUR" if block=="LAKSHMANPUR" & district=="PRATAPGARH" & state=="UTTAR PRADESH"
replace block = "SANGIPUR" if block=="SINGHIPUR" & district=="PRATAPGARH" & state=="UTTAR PRADESH"
replace block = "KAURIHAR" if block=="KAUDIHAAR" & district=="ALLAHABAD" & state=="UTTAR PRADESH"
replace block = "MAUAIMA" if block=="MAU AIMA" & district=="ALLAHABAD" & state=="UTTAR PRADESH"
replace block = "DUBEY PUR" if block=="DUBEPUR" & district=="SULTANPUR" & state=="UTTAR PRADESH"
replace block = "TAJWAPUR" if block=="TEJWAPUR" & district=="BAHRAICH" & state=="UTTAR PRADESH"
replace block = "KHESRAHA" if block=="KHESRHA" & district=="SIDDHARTHNAGAR" & state=="UTTAR PRADESH"
replace block = "NATH NAGAR" if block=="NATHNAGAR" & district=="SANT KABIR NAGAR" & state=="UTTAR PRADESH"
replace block = "JANGAL KODIA" if block=="JANGAL KAUDIA" & district=="GORAKHPUR" & state=="UTTAR PRADESH"
replace block = "KAURI RAM" if block=="KAURIRAM" & district=="GORAKHPUR" & state=="UTTAR PRADESH"
replace block = "PATHAR DEWA" if block=="PATHARDEWA" & district=="DEORIA" & state=="UTTAR PRADESH"
replace block = "BILARIYAGANJ" if block=="BILARIAGANJ" & district=="AZAMGARH" & state=="UTTAR PRADESH"
replace block = "MARTINGANJ" if block=="MATINGANJ" & district=="AZAMGARH" & state=="UTTAR PRADESH"
replace block = "PALHNA" if block=="PALHANA" & district=="AZAMGARH" & state=="UTTAR PRADESH"
replace block = "DOHARI GHAT" if block=="DOHRI GHAT" & district=="MAU" & state=="UTTAR PRADESH"
replace block = "DHARAMPUR" if block=="DHARMAPUR" & district=="JAUNPUR" & state=="UTTAR PRADESH"
replace block = "MOGRA BADSHAHPUR" if block=="MUGRA BADSHAHPUR" & district=="JAUNPUR" & state=="UTTAR PRADESH"
replace block = "SUITHAKALA" if block=="SUITHKALA" & district=="JAUNPUR" & state=="UTTAR PRADESH"
replace block = "MAJHAWA" if block=="MAJHAWAN" & district=="MIRZAPUR" & state=="UTTAR PRADESH"
replace block = "SIKHAR" if block=="SIKHDI" & district=="MIRZAPUR" & state=="UTTAR PRADESH"
replace block = "RAMLE BANGO" if block=="RAMLE-BANGO" & district=="EAST SIANG" & state=="ARUNACHAL PRADESH"
replace block = "ETALIN-MALINEY" if block=="ETALIN-MALINYE" & district=="DIBANG VALLEY" & state=="ARUNACHAL PRADESH"
replace block = "WOZHURO RALAN" if block=="WOZURO-RALAN" & district=="WOKHA" & state=="NAGALAND"
replace block = "CHAPAR-SALKOCHA" if block=="CHAPAP-SALKOCHA" & district=="DHUBRI" & state=="ASSAM"
replace block = "KUSHDHAWA" if block=="KUSHDHWA" & district=="GOALPARA" & state=="ASSAM"
replace block = "CHAKCHAKA" if block=="CHAK-CHAKA" & district=="BARPETA" & state=="ASSAM"
replace block = "SUALKUCHI" if block=="SULKUCHI" & district=="KAMRUP" & state=="ASSAM"
replace block = "BHURBANDHA" if block=="BHUR-BANDHA" & district=="MARIGAON" & state=="ASSAM"
replace block = "KAPILI PT." if block=="KALILI PT." & district=="NAGAON" & state=="ASSAM"
replace block = "LAOKHOWA" if block=="LAO-KHOWA" & district=="NAGAON" & state=="ASSAM"
replace block = "KAKODONGA" if block=="LAKODONGA" & district=="GOLAGHAT" & state=="ASSAM"
replace block = "JATING VALLEY" if block=="JATINGA VALLEY" & district=="NORTH CACHAR HILLS" & state=="ASSAM"
replace block = "BARKHOLA" if block=="BAR-KHOLAA" & district=="CACHAR" & state=="ASSAM"
replace block = "RAJABAZAR" if block=="RAJA BAZAR" & district=="CACHAR" & state=="ASSAM"
replace block = "SALCHAPRA" if block=="SAL-CHAPRA" & district=="CACHAR" & state=="ASSAM"
replace block = "DULLAVCHERRA" if block=="DULLAV CHERRA" & district=="KARIMGANJ" & state=="ASSAM"
replace block = "KUANRMUNDA" if block=="KUARMUNDA" & district=="SUNDARGARH" & state=="ORISSA"
replace block = "SHAMAKHUNTA" if block=="SAMAKHUNTA" & district=="MAYURBHANJ" & state=="ORISSA"
replace block = "SIMILIA" if block=="SIMULIA" & district=="BALESHWAR" & state=="ORISSA"
replace block = "DANAGADI" if block=="DANGADI" & district=="JAJAPUR" & state=="ORISSA"
replace block = "JAJAPUR" if block=="JAJPUR" & district=="JAJAPUR" & state=="ORISSA"
replace block = "BANARPAL" if block=="BANRAPAL" & district=="ANUGUL" & state=="ORISSA"
replace block = "PHULABANI" if block=="PHULBANI" & district=="KANDHAMAL" & state=="ORISSA"
replace block = "PUINTALA" if block=="PUITALA" & district=="BALANGIR" & state=="ORISSA"
replace block = "KOLNARA" if block=="KOLANARA" & district=="RAYAGADA" & state=="ORISSA"
replace block = "KHAIRAPUT" if block=="KHAIRPUT" & district=="MALKANGIRI" & state=="ORISSA"
replace block = "GARIABAND" if block=="GARIYABAND" & district=="RAIPUR" & state=="CHHATTISGARH"
replace block = "GANGEV" if block=="GANGEO" & district=="REWA" & state=="MADHYA PRADESH"
replace block = "KATTIWADA" if block=="KATTHIWADA" & district=="JHABUA" & state=="MADHYA PRADESH"
replace block = "BALDI" if block=="BALADI" & district=="EAST NIMAR" & state=="MADHYA PRADESH"
replace block = "KOOTHUPARAMBA" if block=="KOOTHUPARAMBU" & district=="KANNUR" & state=="KERALA"
replace block = "BALUSSERI" if block=="BNALUSSERY" & district=="KOZHIKODE" & state=="KERALA"
replace block = "CHELANNUR" if block=="CHELANNOOR" & district=="KOZHIKODE" & state=="KERALA"
replace block = "KUNNUMMAL" if block=="KUNNUMMEL" & district=="KOZHIKODE" & state=="KERALA"
replace block = "PANTHALAYANI" if block=="PANDALAYANI" & district=="KOZHIKODE" & state=="KERALA"
replace block = "PERAMBRA" if block=="PERMBRA" & district=="KOZHIKODE" & state=="KERALA"
replace block = "THUNERI" if block=="THOONERI" & district=="KOZHIKODE" & state=="KERALA"
replace block = "AREEKODE" if block=="AREECODE" & district=="MALAPPURAM" & state=="KERALA"
replace block = "KUTTIPPURAM" if block=="KUTTIPURAM" & district=="MALAPPURAM" & state=="KERALA"
replace block = "PERUMPADAPPA" if block=="PERUMPADAPPU" & district=="MALAPPURAM" & state=="KERALA"
replace block = "WANDOOR" if block=="VANDOOR" & district=="MALAPPURAM" & state=="KERALA"
replace block = "NEMMARA" if block=="NENMARA" & district=="PALAKKAD" & state=="KERALA"
replace block = "ELAMDESOM" if block=="ELAMDESAM" & district=="IDUKKI" & state=="KERALA"
replace block = "NEDUMKANDAM" if block=="NEDUMKANDOM" & district=="IDUKKI" & state=="KERALA"
replace block = "KADAMBATTUR" if block=="KADAMBATHUR" & district=="THIRUVALLUR" & state=="TAMIL NADU"
replace block = "R.K.PET" if block=="R. K. PET" & district=="THIRUVALLUR" & state=="TAMIL NADU"
replace block = "TIRUVELANGADU" if block=="THIRUVALANGADU" & district=="THIRUVALLUR" & state=="TAMIL NADU"
replace block = "ACHARAPAKKAM" if block=="ACHIRAPAKKAM" & district=="KANCHEEPURAM" & state=="TAMIL NADU"
replace block = "KATTANKOLATTUR" if block=="KATTANKULATHUR" & district=="KANCHEEPURAM" & state=="TAMIL NADU"
replace block = "THIRUPORUR" if block=="TIRUPORUR" & district=="KANCHEEPURAM" & state=="TAMIL NADU"
replace block = "K.V. KUPPAM" if block=="K V KUPPAM" & district=="VELLORE" & state=="TAMIL NADU"
replace block = "PERNAMPATTU" if block=="PERANAMBATTU" & district=="VELLORE" & state=="TAMIL NADU"
replace block = "THALY" if block=="THALLY" & district=="DHARMAPURI" & state=="TAMIL NADU"
replace block = "JAWATHU HILLS" if block=="JAWADHUHILLS" & district=="TIRUVANNAMALAI" & state=="TAMIL NADU"
replace block = "KILPERNNATHUR" if block=="KILPENNATHUR" & district=="TIRUVANNAMALAI" & state=="TAMIL NADU"
replace block = "CHINNA SALEM" if block=="CHINNASALEM" & district=="VILUPPURAM" & state=="TAMIL NADU"
replace block = "KALRAYAN HILLS" if block=="KALRAYANHILLS" & district=="VILUPPURAM" & state=="TAMIL NADU"
replace block = "KANAI" if block=="KANNAI" & district=="VILUPPURAM" & state=="TAMIL NADU"
replace block = "MELMALAYANUR" if block=="MELMALAIYANUR" & district=="VILUPPURAM" & state=="TAMIL NADU"
replace block = "VIRKRAVANDI" if block=="VIKRAVANDI" & district=="VILUPPURAM" & state=="TAMIL NADU"
replace block = "AYODHIAPATTINAM" if block=="AYOTHIAPATTINAM" & district=="SALEM" & state=="TAMIL NADU"
replace block = "NANGAVALLI" if block=="NAGAVALLI" & district=="SALEM" & state=="TAMIL NADU"
replace block = "PANAIMARATHUPATTI" if block=="PANAMARATHUPATTAI" & district=="SALEM" & state=="TAMIL NADU"
replace block = "PETHANAICKENPALAYAM" if block=="PETHANAIKENPALAYAM" & district=="SALEM" & state=="TAMIL NADU"
replace block = "TALAVASAL" if block=="THALAIVASAL" & district=="SALEM" & state=="TAMIL NADU"
replace block = "ELAICHIPALAYAM" if block=="ELACHIPALAYAM" & district=="NAMAKKAL" & state=="TAMIL NADU"
replace block = "KABILAMALAI" if block=="KABILARMALAI" & district=="NAMAKKAL" & state=="TAMIL NADU"
replace block = "VENNANTHUR" if block=="VENNATHUR" & district=="NAMAKKAL" & state=="TAMIL NADU"
replace block = "TALAVADI" if block=="THALAVADI" & district=="ERODE" & state=="TAMIL NADU"
replace block = "PERIYANAICKENPALAYAM" if block=="PERIYANAICKEN PALAYA" & district=="COIMBATORE" & state=="TAMIL NADU"
replace block = "GUZILIAMPARAI" if block=="GUZILLIAMPARAI" & district=="DINDIGUL" & state=="TAMIL NADU"
replace block = "SHANARPATTI" if block=="SANARPATTI" & district=="DINDIGUL" & state=="TAMIL NADU"
replace block = "UPPILIYAPURAM" if block=="UPPILLIAPURAM" & district=="TIRUCHIRAPPALLI" & state=="TAMIL NADU"
replace block = "KEELAIYUR" if block=="KEEZHAIYUR" & district=="NAGAPATTINAM" & state=="TAMIL NADU"
replace block = "KORADACHERI" if block=="KORADACHERRY" & district=="THIRUVARUR" & state=="TAMIL NADU"
replace block = "ANNAVASAL" if block=="ANNVASAL" & district=="PUDUKKOTTAI" & state=="TAMIL NADU"
replace block = "KUNNANDARKOVIL" if block=="KUNNANDARKOIL" & district=="PUDUKKOTTAI" & state=="TAMIL NADU"
replace block = "TIRUVARANKULAM" if block=="THIRUVARANKULAM" & district=="PUDUKKOTTAI" & state=="TAMIL NADU"
replace block = "KANNANKUDI" if block=="KANNAKUDI" & district=="SIVAGANGA" & state=="TAMIL NADU"
replace block = "S. PUDUR" if block=="S.PUDUR" & district=="SIVAGANGA" & state=="TAMIL NADU"
replace block = "SINGAMPUNERI" if block=="SINGAMPUNARY" & district=="SIVAGANGA" & state=="TAMIL NADU"
replace block = "KALLLIKUDI" if block=="KALLIKUDI" & district=="MADURAI" & state=="TAMIL NADU"
replace block = "T. KALLUPATTI" if block=="T.KALLUPATTI" & district=="MADURAI" & state=="TAMIL NADU"
replace block = "TIRUPULLANI" if block=="TIRUPPULLANI" & district=="RAMANATHAPURAM" & state=="TAMIL NADU"
replace block = "ALWARTHIRUNAGIRI" if block=="ALWARTHIRUNGIRI" & district=="THOOTHUKKUDI" & state=="TAMIL NADU"
replace block = "KARUNKULAM" if block=="KARUNGULAM" & district=="THOOTHUKKUDI" & state=="TAMIL NADU"
replace block = "KURUNTHANCODE" if block=="KURUNTHENCODE" & district=="KANNIYAKUMARI" & state=="TAMIL NADU"
replace block = "THACKALAI" if block=="THAKKALAY" & district=="KANNIYAKUMARI" & state=="TAMIL NADU"
replace block = "PERINTHALMANNA" if block=="PERUMTHALMANNA" & district=="MALAPPURAM" & state=="KERALA"
replace block = "LOHIAN KHAS" if block=="LOHIAN" & district=="JALANDHAR" & state=="PUNJAB"
replace block = "SIRHIND" if block=="SIRHIND MANDI" & district=="FATEHGARH SAHIB" & state=="PUNJAB"
replace block = "SADAURA" if block=="SADAURA (PART)" & district=="YAMUNANAGAR" & state=="HARYANA"
replace block = "BAKANI FOREST VILLAGE" if block=="BAKANI" & district=="JHALAWAR" & state=="RAJASTHAN"
replace block = "BUDHANPUR SEOHARA" if block=="BUDHANPUR" & district=="BIJNOR" & state=="UTTAR PRADESH"
replace block = "HALDAUR (KHARI JHALU)" if block=="HALDAUR" & district=="BIJNOR" & state=="UTTAR PRADESH"
replace block = "JAWAN SIKANDERPUR" if block=="JAWAN" & district=="ALIGARH" & state=="UTTAR PRADESH"
replace block = "KATRA (KHUDAGANJ)" if block=="KATRA" & district=="SHAHJAHANPUR" & state=="UTTAR PRADESH"
replace block = "CHAMBANG" if block=="CHAMBANG" & district=="KURUNG KUMEY" & state=="ARUNACHAL PRADESH"
replace block = "BELLAMPALLE" if block=="BELLAMPALLI" & district=="ADILABAD" & state=="ANDHRA PRADESH"
replace block = "BHIMINI" if block=="BHEEMINI" & district=="ADILABAD" & state=="ANDHRA PRADESH"
replace block = "DANDEPALLE" if block=="DANDEPALLY" & district=="ADILABAD" & state=="ANDHRA PRADESH"
replace block = "GUDIHATHNOOR" if block=="GUDIHATHNUR" & district=="ADILABAD" & state=="ANDHRA PRADESH"
replace block = "JAINAD" if block=="JAINATH" & district=="ADILABAD" & state=="ANDHRA PRADESH"
replace block = "KOUTHALA" if block=="KOWTHALA" & district=="ADILABAD" & state=="ANDHRA PRADESH"
replace block = "MANDAMARRI" if block=="MANDAMARRY" & district=="ADILABAD" & state=="ANDHRA PRADESH"
replace block = "NERADIGONDA" if block=="NEREDIGONDA" & district=="ADILABAD" & state=="ANDHRA PRADESH"
replace block = "TANDUR" if block=="TANDOOR" & district=="ADILABAD" & state=="ANDHRA PRADESH"
replace block = "VEMANPALLE" if block=="VEMANPALLY" & district=="ADILABAD" & state=="ANDHRA PRADESH"
replace block = "CHENNEKOTHAPALLE" if block=="CHENNE KOTHAPALLE" & district=="ANANTAPUR" & state=="ANDHRA PRADESH"
replace block = "PENUKONDA" if block=="PENU KONDA" & district=="ANANTAPUR" & state=="ANDHRA PRADESH"
replace block = "BAIREDDIPALLE" if block=="BAIREDDI PALLE" & district=="CHITTOOR" & state=="ANDHRA PRADESH"
replace block = "GUDUPALLE" if block=="GUDI PALLE" & district=="CHITTOOR" & state=="ANDHRA PRADESH"
replace block = "MULAKALACHERUVU" if block=="MOLAKALACHERUVU" & district=="CHITTOOR" & state=="ANDHRA PRADESH"
replace block = "RAMAKUPPAM" if block=="RAMA KUPPAM" & district=="CHITTOOR" & state=="ANDHRA PRADESH"
replace block = "SANTHIPURAM" if block=="SANTHI PURAM" & district=="CHITTOOR" & state=="ANDHRA PRADESH"
replace block = "THAMBALLAPALLE" if block=="THAMBALLAPALLE  " & district=="CHITTOOR" & state=="ANDHRA PRADESH"
replace block = "VEDURUKUPPAM" if block=="VEDURU KUPPAM" & district=="CHITTOOR" & state=="ANDHRA PRADESH"
replace block = "VENKATAGIRIKOTA" if block=="VENKATAGIRI KOTA" & district=="CHITTOOR" & state=="ANDHRA PRADESH"
replace block = "VIJAYAPURAM" if block=="VIJAYA PURAM" & district=="CHITTOOR" & state=="ANDHRA PRADESH"
replace block = "ATLUR" if block=="ATLOOR" & district=="CUDDAPAH" & state=="ANDHRA PRADESH"
replace block = "B.KODUR" if block=="B. KODUR" & district=="CUDDAPAH" & state=="ANDHRA PRADESH"
replace block = "KHAJIPET" if block=="KHAJIPETA" & district=="CUDDAPAH" & state=="ANDHRA PRADESH"
replace block = "LAKKIREDDIPALLE" if block=="LAKKIREDDIPALLI" & district=="CUDDAPAH" & state=="ANDHRA PRADESH"
replace block = "MUDDANUR" if block=="MUDDANURU" & district=="CUDDAPAH" & state=="ANDHRA PRADESH"
replace block = "OBULAVARIPALLE" if block=="OBULAVARIPALLI" & district=="CUDDAPAH" & state=="ANDHRA PRADESH"
replace block = "PEDDAMUDIUM" if block=="PEDDAMUDIAM" & district=="CUDDAPAH" & state=="ANDHRA PRADESH"
replace block = "PULIVENDLA" if block=="PULIVENDULA" & district=="CUDDAPAH" & state=="ANDHRA PRADESH"
replace block = "RAYACHOTI" if block=="RAYACHOTY" & district=="CUDDAPAH" & state=="ANDHRA PRADESH"
replace block = "SAMBEPALLE" if block=="SAMBEPALLI" & district=="CUDDAPAH" & state=="ANDHRA PRADESH"
replace block = "T SUNDUPALLE" if block=="T.SUNDUPALLI" & district=="CUDDAPAH" & state=="ANDHRA PRADESH"
replace block = "VEERABALLE" if block=="VEERABALLI" & district=="CUDDAPAH" & state=="ANDHRA PRADESH"
replace block = "VEERAPUNAYUNIPALLE" if block=="VEERAPUNAYUNIPALLI" & district=="CUDDAPAH" & state=="ANDHRA PRADESH"
replace block = "VEMPALLE" if block=="VEMPALLI" & district=="CUDDAPAH" & state=="ANDHRA PRADESH"
replace block = "AINAVILLI" if block=="AINAVILLI  " & district=="EAST GODAVARI" & state=="ANDHRA PRADESH"
replace block = "GANDEPALLE" if block=="GANDEPALLI" & district=="EAST GODAVARI" & state=="ANDHRA PRADESH"
replace block = "MALIKIPURAM" if block=="MALKIPURAM" & district=="EAST GODAVARI" & state=="ANDHRA PRADESH"
replace block = "MAMIDIKUDURU" if block=="MAMIDIKURURU" & district=="EAST GODAVARI" & state=="ANDHRA PRADESH"
replace block = "MAREDUMILLI" if block=="MAREDIMILLI" & district=="EAST GODAVARI" & state=="ANDHRA PRADESH"
replace block = "SAKHINETIPALLE" if block=="SAKHINETIPALLI" & district=="EAST GODAVARI" & state=="ANDHRA PRADESH"
replace block = "BOLLAPALLE" if block=="BOLLAPALLI" & district=="GUNTUR" & state=="ANDHRA PRADESH"
replace block = "DACHEPALLE" if block=="DACHEPALLI" & district=="GUNTUR" & state=="ANDHRA PRADESH"
replace block = "DUGGIRALA" if block=="DUGGIRALAI" & district=="GUNTUR" & state=="ANDHRA PRADESH"
replace block = "PONNUR" if block=="PONNURU" & district=="GUNTUR" & state=="ANDHRA PRADESH"
replace block = "SATTENAPALLE" if block=="SATTENAPALLI" & district=="GUNTUR" & state=="ANDHRA PRADESH"
replace block = "TADEPALLE" if block=="TADEPALLI" & district=="GUNTUR" & state=="ANDHRA PRADESH"
replace block = "BOINPALLE" if block=="BOINPALLY" & district=="KARIMNAGAR" & state=="ANDHRA PRADESH"
replace block = "CHANDURTHI" if block=="CHANDURTHY" & district=="KARIMNAGAR" & state=="ANDHRA PRADESH"
replace block = "GOLLAPALLE" if block=="GOLLAPALLY" & district=="KARIMNAGAR" & state=="ANDHRA PRADESH"
replace block = "JULAPALLE" if block=="JULAPALLI" & district=="KARIMNAGAR" & state=="ANDHRA PRADESH"
replace block = "KAMANPUR" if block=="KAMANPOOR" & district=="KARIMNAGAR" & state=="ANDHRA PRADESH"
replace block = "KATHLAPUR" if block=="KATHLAPOOR" & district=="KARIMNAGAR" & state=="ANDHRA PRADESH"
replace block = "KONARAOPETA" if block=="KONARAOPET" & district=="KARIMNAGAR" & state=="ANDHRA PRADESH"
replace block = "MAHADEVPUR" if block=="MAHADEVPOOR" & district=="KARIMNAGAR" & state=="ANDHRA PRADESH"
replace block = "METPALLE" if block=="METPALLY" & district=="KARIMNAGAR" & state=="ANDHRA PRADESH"
replace block = "MUSTABAD" if block=="MUSTHABAD" & district=="KARIMNAGAR" & state=="ANDHRA PRADESH"
replace block = "PEGADAPALLE" if block=="PEGADAPALLY" & district=="KARIMNAGAR" & state=="ANDHRA PRADESH"
replace block = "SRIRAMPUR" if block=="SRIRAMPOOR" & district=="KARIMNAGAR" & state=="ANDHRA PRADESH"
replace block = "YELLAREDDIPET" if block=="YELLAREDDYPET" & district=="KARIMNAGAR" & state=="ANDHRA PRADESH"
replace block = "BURGAMPAHAD" if block=="BURGAMPAD" & district=="KHAMMAM" & state=="ANDHRA PRADESH"
replace block = "CHINTHAKANI" if block=="CHINTAKANI" & district=="KHAMMAM" & state=="ANDHRA PRADESH"
replace block = "CHINTUR" if block=="CHINTOOR" & district=="KHAMMAM" & state=="ANDHRA PRADESH"
replace block = "DUMMUGUDEM" if block=="DUMMAGUDEM" & district=="KHAMMAM" & state=="ANDHRA PRADESH"
replace block = "KAMEPALLE" if block=="KAMEPALLY" & district=="KHAMMAM" & state=="ANDHRA PRADESH"
replace block = "MULKALAPALLE" if block=="MULAKALAPALLY" & district=="KHAMMAM" & state=="ANDHRA PRADESH"
replace block = "NELAKONDAPALLE" if block=="NELAKONDAPALLY" & district=="KHAMMAM" & state=="ANDHRA PRADESH"
replace block = "PENUBALLI" if block=="PENUBALLY" & district=="KHAMMAM" & state=="ANDHRA PRADESH"
replace block = "SATHUPALLE" if block=="SATHUPALLY" & district=="KHAMMAM" & state=="ANDHRA PRADESH"
replace block = "TEKULAPALLE" if block=="TEKULAPALLI" & district=="KHAMMAM" & state=="ANDHRA PRADESH"
replace block = "WAZEED" if block=="WAZEEDU" & district=="KHAMMAM" & state=="ANDHRA PRADESH"
replace block = "AGIRIPALLE" if block=="AGIRIPALLI" & district=="KRISHNA" & state=="ANDHRA PRADESH"
replace block = "CHALLAPALLE" if block=="CHALLAPALLI" & district=="KRISHNA" & state=="ANDHRA PRADESH"
replace block = "KAIKALUR" if block=="KAIKALURU" & district=="KRISHNA" & state=="ANDHRA PRADESH"
replace block = "MUDINEPALLE" if block=="MUDINEPALLI" & district=="KRISHNA" & state=="ANDHRA PRADESH"
replace block = "NUZVID" if block=="NUZVIDU" & district=="KRISHNA" & state=="ANDHRA PRADESH"
replace block = "BANAGANAPALLE" if block=="BANAGANAPALLI" & district=="KURNOOL" & state=="ANDHRA PRADESH"
replace block = "JUPADU BUNGALOW" if block=="JUPADUBUNGLOW" & district=="KURNOOL" & state=="ANDHRA PRADESH"
replace block = "KOILKUNTLA" if block=="KOILAKUNTLA" & district=="KURNOOL" & state=="ANDHRA PRADESH"
replace block = "KOTHAPALLE" if block=="KOTHAPALLI" & district=="KURNOOL" & state=="ANDHRA PRADESH"
replace block = "PEAPALLY" if block=="PEAPULLY" & district=="KURNOOL" & state=="ANDHRA PRADESH"
replace block = "PEDDA KADUBUR" if block=="PEDDAKADUBUR" & district=="KURNOOL" & state=="ANDHRA PRADESH"
replace block = "VELDURTHI" if block=="VELDURTHY" & district=="KURNOOL" & state=="ANDHRA PRADESH"
replace block = "BALANAGAR" if block=="BALANGAR" & district=="MAHBUBNAGAR" & state=="ANDHRA PRADESH"
replace block = "BIJINAPALLE" if block=="BIJINEPALLY" & district=="MAHBUBNAGAR" & state=="ANDHRA PRADESH"
replace block = "CHINNACHINTAKUNTA" if block=="CHINACHINTAKUNTA" & district=="MAHBUBNAGAR" & state=="ANDHRA PRADESH"
replace block = "DAMARAGIDDA" if block=="DAMARGIDDA" & district=="MAHBUBNAGAR" & state=="ANDHRA PRADESH"
replace block = "DOULATHABAD" if block=="DOULTABAD" & district=="MAHBUBNAGAR" & state=="ANDHRA PRADESH"
replace block = "GOPALPETA" if block=="GOPALPET" & district=="MAHBUBNAGAR" & state=="ANDHRA PRADESH"
replace block = "HANWADA" if block=="HUNWADA" & district=="MAHBUBNAGAR" & state=="ANDHRA PRADESH"
replace block = "IEEJ" if block=="IEEZ" & district=="MAHBUBNAGAR" & state=="ANDHRA PRADESH"
replace block = "PEDDAKOTHAPALLE" if block=="PEDDAKOTHAPALLY" & district=="MAHBUBNAGAR" & state=="ANDHRA PRADESH"
replace block = "TALAKONDAPALLE" if block=="TALAKONDAPALLY" & district=="MAHBUBNAGAR" & state=="ANDHRA PRADESH"
replace block = "TELKAPALLE" if block=="TELKAPALLY" & district=="MAHBUBNAGAR" & state=="ANDHRA PRADESH"
replace block = "CHINNAKODUR" if block=="CHINNAKODURU" & district=="MEDAK" & state=="ANDHRA PRADESH"
replace block = "JAGDEVPUR" if block=="JAGADEVPUR" & district=="MEDAK" & state=="ANDHRA PRADESH"
replace block = "JHARASANGAM" if block=="JARASANGAM" & district=="MEDAK" & state=="ANDHRA PRADESH"
replace block = "KOHIR" if block=="KOHEER" & district=="MEDAK" & state=="ANDHRA PRADESH"
replace block = "KOWDIPALLE" if block=="KOWDIPALLY" & district=="MEDAK" & state=="ANDHRA PRADESH"
replace block = "MUNPALLE" if block=="MUNIPALLY" & district=="MEDAK" & state=="ANDHRA PRADESH"
replace block = "TUPRAN" if block=="TOOPRAN" & district=="MEDAK" & state=="ANDHRA PRADESH"
replace block = "ZAHIRABAD" if block=="ZAHEERABAD" & district=="MEDAK" & state=="ANDHRA PRADESH"
replace block = "CHANDAM PET" if block=="CHANDEMPET" & district=="NALGONDA" & state=="ANDHRA PRADESH"
replace block = "CHINTHA PALLE" if block=="CHINTHAPALLY" & district=="NALGONDA" & state=="ANDHRA PRADESH"
replace block = "CHITYALA" if block=="CHITYAL" & district=="NALGONDA" & state=="ANDHRA PRADESH"
replace block = "DAMERACHERLA" if block=="DAMERCHERLA" & district=="NALGONDA" & state=="ANDHRA PRADESH"
replace block = "DEVARAKONDA" if block=="DEVERKONDA" & district=="NALGONDA" & state=="ANDHRA PRADESH"
replace block = "GARIDE PALLE" if block=="GARIDEPALLY" & district=="NALGONDA" & state=="ANDHRA PRADESH"
replace block = "GUNDLA PALLE" if block=="GUNDLAPALLY" & district=="NALGONDA" & state=="ANDHRA PRADESH"
replace block = "GURRAMPODE" if block=="GURREMPODE" & district=="NALGONDA" & state=="ANDHRA PRADESH"
replace block = "KANGAL" if block=="KANAGAL" & district=="NALGONDA" & state=="ANDHRA PRADESH"
replace block = "KATTANGOOR" if block=="KATTANGUR" & district=="NALGONDA" & state=="ANDHRA PRADESH"
replace block = "KETHE PALLE" if block=="KETHEPALLY" & district=="NALGONDA" & state=="ANDHRA PRADESH"
replace block = "MATTAM PALLE" if block=="MATTAMPALLY" & district=="NALGONDA" & state=="ANDHRA PRADESH"
replace block = "MELLA CHERUVU" if block=="MELLACHERUVU" & district=="NALGONDA" & state=="ANDHRA PRADESH"
replace block = "MIRYALAGUDA" if block=="MIRYALGUDA" & district=="NALGONDA" & state=="ANDHRA PRADESH"
replace block = "NAMPALLE" if block=="NAMPALLY" & district=="NALGONDA" & state=="ANDHRA PRADESH"
replace block = "NARKETPALLE" if block=="NARKETPALLY" & district=="NALGONDA" & state=="ANDHRA PRADESH"
replace block = "NEREDCHERLA" if block=="NEREDCHARLA" & district=="NALGONDA" & state=="ANDHRA PRADESH"
replace block = "NUTHANKAL" if block=="NUTHAN KAL" & district=="NALGONDA" & state=="ANDHRA PRADESH"
replace block = "RAMANNAPETA" if block=="RAMANNAPET" & district=="NALGONDA" & state=="ANDHRA PRADESH"
replace block = "THIPPARTHI" if block=="THIPARTHY" & district=="NALGONDA" & state=="ANDHRA PRADESH"
replace block = "THUNGATHURTHI" if block=="THUNGATHURTHY" & district=="NALGONDA" & state=="ANDHRA PRADESH"
replace block = "VEMULAPALLE" if block=="VEMULAPALLY" & district=="NALGONDA" & state=="ANDHRA PRADESH"
replace block = "ANANTHASAGARAM" if block=="ANANTASAGARAM" & district=="NELLORE" & state=="ANDHRA PRADESH"
replace block = "BALAYAPALLE" if block=="BALAYAPALLI" & district=="NELLORE" & state=="ANDHRA PRADESH"
replace block = "BUCHIREDDIPALEM" if block=="BUCHIREDDY PALEM" & district=="NELLORE" & state=="ANDHRA PRADESH"
replace block = "MUTHUKUR" if block=="MUTHUKURU" & district=="NELLORE" & state=="ANDHRA PRADESH"
replace block = "NAIDUPET" if block=="NAIDUPETA" & district=="NELLORE" & state=="ANDHRA PRADESH"
replace block = "SEETHARAMAPURAM" if block=="SEETHA RAMAPURAM" & district=="NELLORE" & state=="ANDHRA PRADESH"
replace block = "SULLURPETA" if block=="SULLURPET" & district=="NELLORE" & state=="ANDHRA PRADESH"
replace block = "ARMUR" if block=="ARMOOR" & district=="NIZAMABAD" & state=="ANDHRA PRADESH"
replace block = "BIRKOOR" if block=="BIRKUR" & district=="NIZAMABAD" & state=="ANDHRA PRADESH"
replace block = "DHARPALLE" if block=="DHARPALLY" & district=="NIZAMABAD" & state=="ANDHRA PRADESH"
replace block = "DICHPALLE" if block=="DICHPALLY" & district=="NIZAMABAD" & state=="ANDHRA PRADESH"
replace block = "JAKRANPALLE" if block=="JAKRANPALLY" & district=="NIZAMABAD" & state=="ANDHRA PRADESH"
replace block = "KAMMARPALLE" if block=="KAMMARPALLY" & district=="NIZAMABAD" & state=="ANDHRA PRADESH"
replace block = "KOTGIRI" if block=="KOTAGIRI" & district=="NIZAMABAD" & state=="ANDHRA PRADESH"
replace block = "MORTAD" if block=="MORTHAD" & district=="NIZAMABAD" & state=="ANDHRA PRADESH"
replace block = "RANJAL" if block=="RENJAL" & district=="NIZAMABAD" & state=="ANDHRA PRADESH"
replace block = "SIRKONDA" if block=="SIRIKONDA" & district=="NIZAMABAD" & state=="ANDHRA PRADESH"
replace block = "YEDPALLE" if block=="YEDPALLY" & district=="NIZAMABAD" & state=="ANDHRA PRADESH"
replace block = "HANUMANTHUNI PADU" if block=="HANUMANTHUNIPADU" & district=="PRAKASAM" & state=="ANDHRA PRADESH"
replace block = "KANDUKUR" if block=="KANDUKURU" & district=="PRAKASAM" & state=="ANDHRA PRADESH"
replace block = "KOMAROLU" if block=="KOMAROLE" & district=="PRAKASAM" & state=="ANDHRA PRADESH"
replace block = "KONAKANAMITLA" if block=="KONAKANAMITTA" & district=="PRAKASAM" & state=="ANDHRA PRADESH"
replace block = "KONDAPI" if block=="KONDEPI" & district=="PRAKASAM" & state=="ANDHRA PRADESH"
replace block = "KOTHA PATNAM" if block=="KOTHAPATNAM" & district=="PRAKASAM" & state=="ANDHRA PRADESH"
replace block = "MADDIPADU" if block=="MADDI PADU" & district=="PRAKASAM" & state=="ANDHRA PRADESH"
replace block = "MUNDLAMURU" if block=="MUNDLAMUR" & district=="PRAKASAM" & state=="ANDHRA PRADESH"
replace block = "NAGULUPPALA PADU" if block=="NAGULUPPALAPADU" & district=="PRAKASAM" & state=="ANDHRA PRADESH"
replace block = "PARCHUR" if block=="PARCHOOR" & district=="PRAKASAM" & state=="ANDHRA PRADESH"
replace block = "PEDA ARAVEEDU" if block=="PEDAARAVEEDU" & district=="PRAKASAM" & state=="ANDHRA PRADESH"
replace block = "RACHERLA" if block=="RACHARLA" & district=="PRAKASAM" & state=="ANDHRA PRADESH"
replace block = "SANTHANUTHALA PADU" if block=="SANTHANUTHALAPADU" & district=="PRAKASAM" & state=="ANDHRA PRADESH"
replace block = "THALLUR" if block=="TALLUR" & district=="PRAKASAM" & state=="ANDHRA PRADESH"
replace block = "YERRAGONDAPALEM" if block=="YERRAGONDA PALEM" & district=="PRAKASAM" & state=="ANDHRA PRADESH"
replace block = "ZARUGUMILLI" if block=="ZARUGUMALLI" & district=="PRAKASAM" & state=="ANDHRA PRADESH"
replace block = "KAVITI" if block=="KAVITY" & district=="SRIKAKULAM" & state=="ANDHRA PRADESH"
replace block = "KOTHURU" if block=="KOTTURU" & district=="SRIKAKULAM" & state=="ANDHRA PRADESH"
replace block = "SANTHAKAVITI" if block=="SANTHAKAVITY" & district=="SRIKAKULAM" & state=="ANDHRA PRADESH"
replace block = "SARUBUJJILI" if block=="SARUBUJJLI" & district=="SRIKAKULAM" & state=="ANDHRA PRADESH"
replace block = "VAJRAPUKOTHURU" if block=="VAJRAPU KOTTURU" & district=="SRIKAKULAM" & state=="ANDHRA PRADESH"
replace block = "ANAKAPALLE" if block=="ANAKAPALLI" & district=="VISAKHAPATNAM" & state=="ANDHRA PRADESH"
replace block = "ANANTHAGIRI" if block=="ANATHAGIRI" & district=="VISAKHAPATNAM" & state=="ANDHRA PRADESH"
replace block = "ARAKU VALLEY" if block=="ARAKUVALLEY" & district=="VISAKHAPATNAM" & state=="ANDHRA PRADESH"
replace block = "DEVARAPALLE" if block=="DEVARAPALLI" & district=="VISAKHAPATNAM" & state=="ANDHRA PRADESH"
replace block = "KOTAURATLA" if block=="KOTAVURATLA" & district=="VISAKHAPATNAM" & state=="ANDHRA PRADESH"
replace block = "MUNCHINGI PUTTU" if block=="MUNCHINGIPUTTU" & district=="VISAKHAPATNAM" & state=="ANDHRA PRADESH"
replace block = "NAKKAPALLE" if block=="NAKKAPALLI" & district=="VISAKHAPATNAM" & state=="ANDHRA PRADESH"
replace block = "PAYAKARAOPETA" if block=="PAYAKARAO PETA" & district=="VISAKHAPATNAM" & state=="ANDHRA PRADESH"
replace block = "PEDA BAYALU" if block=="PEDABAYALU" & district=="VISAKHAPATNAM" & state=="ANDHRA PRADESH"
replace block = "BONDAPALLE" if block=="BONDAPALLI" & district=="VIZIANAGARAM" & state=="ANDHRA PRADESH"
replace block = "CHEEPURUPALLE" if block=="CHEEPURUPALLI" & district=="VIZIANAGARAM" & state=="ANDHRA PRADESH"
replace block = "GAJAPATHINAGARAM" if block=="GAJAPATINAGARAM" & district=="VIZIANAGARAM" & state=="ANDHRA PRADESH"
replace block = "KOTHAVALASA" if block=="KOTTAVALASA" & district=="VIZIANAGARAM" & state=="ANDHRA PRADESH"
replace block = "MERAKAMUDIDAM" if block=="MERAKAMUDDIDAM" & district=="VIZIANAGARAM" & state=="ANDHRA PRADESH"
replace block = "SALUR" if block=="SALURU" & district=="VIZIANAGARAM" & state=="ANDHRA PRADESH"
replace block = "ATMAKUR" if block=="ATHMAKUR" & district=="WARANGAL" & state=="ANDHRA PRADESH"
replace block = "BACHANNAPET" if block=="BACHANNAPETA" & district=="WARANGAL" & state=="ANDHRA PRADESH"
replace block = "BHUPALPALLE" if block=="BHUPALPALLI" & district=="WARANGAL" & state=="ANDHRA PRADESH"
replace block = "HANAMKONDA" if block=="HANUMAKONDA" & district=="WARANGAL" & state=="ANDHRA PRADESH"
replace block = "HASANPARTHY" if block=="HASANPARTHI" & district=="WARANGAL" & state=="ANDHRA PRADESH"
replace block = "JANGAON" if block=="JANAGAON" & district=="WARANGAL" & state=="ANDHRA PRADESH"
replace block = "KOTHAGUDEM" if block=="KOTHAGUDA" & district=="WARANGAL" & state=="ANDHRA PRADESH"
replace block = "MOGULLAPALLE" if block=="MOGULLAPALLI" & district=="WARANGAL" & state=="ANDHRA PRADESH"
replace block = "NALLABELLY" if block=="NALLABELLI" & district=="WARANGAL" & state=="ANDHRA PRADESH"
replace block = "PALAKURTHI" if block=="PALAKURTHY" & district=="WARANGAL" & state=="ANDHRA PRADESH"
replace block = "RAGHUNATHPALLE" if block=="RAGHUNATHAPALLI" & district=="WARANGAL" & state=="ANDHRA PRADESH"
replace block = "WARDHANNA PET" if block=="WARDHANNAPET" & district=="WARANGAL" & state=="ANDHRA PRADESH"
replace block = "BUTTAYAGUDEM" if block=="BUTTAIGUDEM" & district=="WEST GODAVARI" & state=="ANDHRA PRADESH"
replace block = "DEVARAPALLE" if block=="DEVARAPALLI" & district=="WEST GODAVARI" & state=="ANDHRA PRADESH"
replace block = "JEELUGU MILLI" if block=="JEELUGUMILLI" & district=="WEST GODAVARI" & state=="ANDHRA PRADESH"
replace block = "MAYONG" if block=="MAYANG" & district=="MARIGAON" & state=="ASSAM"
replace block = "MALKHARODA" if block=="MALKHARAUDA" & district=="JANJGIR - CHAMPA" & state=="CHHATTISGARH"
replace block = "GHARGHODA" if block=="GHARGODA" & district=="RAIGARH" & state=="CHHATTISGARH"
replace block = "PRATAPPUR" if block=="PRATAPUR" & district=="SURGUJA" & state=="CHHATTISGARH"
replace block = "BARWALA" if block=="BARVALA" & district=="AHMADABAD" & state=="GUJARAT"
replace block = "RANPUR" if block=="RAMPURA" & district=="AHMADABAD" & state=="GUJARAT"
replace block = "SAVAR KUNDLA" if block=="SAVERKUNDLA" & district=="AMRELI" & state=="GUJARAT"
replace block = "DANTIWADA" if block=="DANTIVADA" & district=="BANAS KANTHA" & state=="GUJARAT"
replace block = "DEVGADBARIA" if block=="DEVGAD BARIYA" & district=="DOHAD" & state=="GUJARAT"
replace block = "PATAN-VERAVAL" if block=="PATAN VERAVAL" & district=="JUNAGADH" & state=="GUJARAT"
replace block = "BECHARAJI" if block=="BECHRAJI" & district=="MAHESANA" & state=="GUJARAT"
replace block = "SATLASANA" if block=="SATLASNA" & district=="MAHESANA" & state=="GUJARAT"
replace block = "DEDIAPADA" if block=="DEDIYAPADA" & district=="NARMADA" & state=="GUJARAT"
replace block = "CHIKHLI" if block=="CHIKHALI" & district=="NAVSARI" & state=="GUJARAT"
replace block = "CHHOTA UDAIPUR" if block=="CHHOTA UDEPUR" & district=="VADODARA" & state=="GUJARAT"
replace block = "KAVANT" if block=="KAWANT" & district=="VADODARA" & state=="GUJARAT"
replace block = "NASVADI" if block=="NASWADI" & district=="VADODARA" & state=="GUJARAT"
replace block = "SINOR" if block=="SHINOR" & district=="VADODARA" & state=="GUJARAT"
replace block = "VAGHODIA" if block=="WAGHODIA" & district=="VADODARA" & state=="GUJARAT"
replace block = "DHANBAD-CUM-KENDUADIH-CUM-JAGTA" if block=="DHANBAD-CUM-KENDUADIH-CUM-JOGT" & district=="DHANBAD" & state=="JHARKHAND"
replace block = "JHARIA-CUM-JORAPOKHAR-CUM-SINDRI" if block=="JHARIA-CUM-JORAPOKHAR-CUM-SIND" & district=="DHANBAD" & state=="JHARKHAND"
replace block = "SHIKARIPARA" if block=="SIKARIPARA" & district=="DUMKA" & state=="JHARKHAND"
replace block = "KODARMA" if block=="KODERMA" & district=="KODARMA" & state=="JHARKHAND"
replace block = "LITIPARA" if block=="LITTIPARA" & district=="PAKAUR" & state=="JHARKHAND"
replace block = "PAKAUR" if block=="PAKUR" & district=="PAKAUR" & state=="JHARKHAND"
replace block = "LESLIEGANJ" if block=="LESLIGANJ" & district=="PALAMU" & state=="JHARKHAND"
replace block = "ICHAGARH" if block=="ICHA GARH" & district=="PASHCHIMI SINGHBHUM" & state=="JHARKHAND"
replace block = "DEVADURGA" if block=="DEODURGA" & district=="RAICHUR" & state=="KARNATAKA"
replace block = "JOURA" if block=="JAURA" & district=="MORENA" & state=="MADHYA PRADESH"
replace block = "UNCHEHARA" if block=="UNCHEHRA" & district=="SATNA" & state=="MADHYA PRADESH"
replace block = "JHIRANYA" if block=="ZIRANYA" & district=="WEST NIMAR" & state=="MADHYA PRADESH"
replace block = "RAHTA" if block=="RAHATA" & district=="AHMADNAGAR" & state=="MAHARASHTRA"
replace block = "BID" if block=="BEED" & district=="BID" & state=="MAHARASHTRA"
replace block = "CHIKHLI" if block=="CHIKHALI" & district=="BULDANA" & state=="MAHARASHTRA"
replace block = "DEOLGAON RAJA" if block=="DEULGAON RAJA" & district=="BULDANA" & state=="MAHARASHTRA"
replace block = "NAGBHIR" if block=="NAGBHID" & district=="CHANDRAPUR" & state=="MAHARASHTRA"
replace block = "GONDIYA" if block=="GONDIA" & district=="GONDIYA" & state=="MAHARASHTRA"
replace block = "SADAK-ARJUNI" if block=="SADAK ARJUNI" & district=="GONDIYA" & state=="MAHARASHTRA"
replace block = "JAFFERABAD" if block=="JAFRABAD" & district=="JALNA" & state=="MAHARASHTRA"
replace block = "HATKANANGLE" if block=="HATKANANGALE" & district=="KOLHAPUR" & state=="MAHARASHTRA"
replace block = "SHIRUR-ANANTPAL" if block=="SHIRUR ANANTPAL" & district=="LATUR" & state=="MAHARASHTRA"
replace block = "UMRI" if block=="UMARI" & district=="NANDED" & state=="MAHARASHTRA"
replace block = "AKRANI" if block=="AKARANI" & district=="NANDURBAR" & state=="MAHARASHTRA"
replace block = "MANWATH" if block=="MANAWATH" & district=="PARBHANI" & state=="MAHARASHTRA"
replace block = "SAILU" if block=="SHELU" & district=="PARBHANI" & state=="MAHARASHTRA"
replace block = "SHRIVARDHAN" if block=="SHRIWARDHAN" & district=="RAIGARH" & state=="MAHARASHTRA"
replace block = "JAT" if block=="JATH" & district=="SANGLI" & state=="MAHARASHTRA"
replace block = "KAVATHE-MAHANKAL" if block=="KAWATHE MAHANKAL" & district=="SANGLI" & state=="MAHARASHTRA"
replace block = "KANKAVLI" if block=="KANKAWALI" & district=="SINDHUDURG" & state=="MAHARASHTRA"
replace block = "SANGOLE" if block=="SANGOLA" & district=="SOLAPUR" & state=="MAHARASHTRA"
replace block = "VADA" if block=="WADA" & district=="THANE" & state=="MAHARASHTRA"
replace block = "BABULGAON" if block=="BABHULGAON" & district=="YAVATMAL" & state=="MAHARASHTRA"
replace block = "ZARI-JAMANI" if block=="ZARI JAMNI" & district=="YAVATMAL" & state=="MAHARASHTRA"
replace block = "LOISINGA" if block=="LOISINGHA" & district=="BALANGIR" & state=="ORISSA"
replace block = "TUREKELA" if block=="TUREIKELA" & district=="BALANGIR" & state=="ORISSA"
replace block = "HARBHANGA" if block=="HARABHANGA" & district=="BAUDH" & state=="ORISSA"
replace block = "BARKOT" if block=="BARKOTE" & district=="DEBAGARH" & state=="ORISSA"
replace block = "JAGATSINGHAPUR" if block=="JAGATSINGHPUR" & district=="JAGATSINGHAPUR" & state=="ORISSA"
replace block = "NAUGAON" if block=="NUAGAON" & district=="JAGATSINGHAPUR" & state=="ORISSA"
replace block = "KOLIANA" if block=="KULIANA" & district=="MAYURBHANJ" & state=="ORISSA"
replace block = "SULIAPADA" if block=="SULEIPADA" & district=="MAYURBHANJ" & state=="ORISSA"
replace block = "JHARIGAN" if block=="JHARIGAM" & district=="NABARANGAPUR" & state=="ORISSA"
replace block = "BODEN" if block=="BADEN" & district=="NUAPADA" & state=="ORISSA"
replace block = "KOMANA" if block=="KOMNA" & district=="NUAPADA" & state=="ORISSA"
replace block = "JUJOMURA" if block=="JUJUMURA" & district=="SAMBALPUR" & state=="ORISSA"
replace block = "KOCHINDA" if block=="KUCHINDA" & district=="SAMBALPUR" & state=="ORISSA"
replace block = "BIRAMAHARAJPUR" if block=="BIRMAHARAJPUR" & district=="SONAPUR" & state=="ORISSA"
replace block = "SONAPUR" if block=="SONEPUR" & district=="SONAPUR" & state=="ORISSA"
replace block = "TARBHA" if block=="TARVA" & district=="SONAPUR" & state=="ORISSA"
replace block = "MANDALGARH" if block=="MANDALGAR" & district=="BHILWARA" & state=="RAJASTHAN"
replace block = "UDAIPURWATI" if block=="UDAIPURWA" & district=="JHUNJHUNUN" & state=="RAJASTHAN"
replace block = "KUMBHALGARH" if block=="KUMBHALGA" & district=="RAJSAMAND" & state=="RAJASTHAN"
replace block = "JAMPUII HILLS" if block=="JAMPUI HILLS" & district=="NORTH TRIPURA" & state=="TRIPURA"
replace block = "JAMPUIJALA" if block=="JAMPAIJALA" & district=="WEST TRIPURA" & state=="TRIPURA"
replace block = "TULASHIKHAR" if block=="TULASIKHAR" & district=="WEST TRIPURA" & state=="TRIPURA"
replace block = "KARCHHANA" if block=="KARCHANA" & district=="ALLAHABAD" & state=="UTTAR PRADESH"
replace block = "MEHNAGAR" if block=="MEH NAGAR" & district=="AZAMGARH" & state=="UTTAR PRADESH"
replace block = "KAISERGANJ" if block=="KAISARGANJ" & district=="BAHRAICH" & state=="UTTAR PRADESH"
replace block = "HAIDERGARH" if block=="HAIDARGARH" & district=="BARABANKI" & state=="UTTAR PRADESH"
replace block = "CHAKIA" if block=="CHAKIYA" & district=="CHANDAULI" & state=="UTTAR PRADESH"
replace block = "JAKHANIAN" if block=="JAKHANIA" & district=="GHAZIPUR" & state=="UTTAR PRADESH"
replace block = "CAMPIERGANJ" if block=="CAMPIERE GANJ" & district=="GORAKHPUR" & state=="UTTAR PRADESH"
replace block = "MAHRONI" if block=="MAHRAUNI" & district=="LALITPUR" & state=="UTTAR PRADESH"
replace block = "BAKSHI KA TALAB" if block=="BAKSHI-KA-TALAB" & district=="LUCKNOW" & state=="UTTAR PRADESH"
replace block = "MAHARAJGANJ" if block=="MAHRAJGANJ" & district=="MAHRAJGANJ" & state=="UTTAR PRADESH"
replace block = "NAUTANWA" if block=="NAUTANAWA" & district=="MAHRAJGANJ" & state=="UTTAR PRADESH"
replace block = "MUHAMMADABAD GOHNA" if block=="MUHAMMADABAD GOHANA" & district=="MAU" & state=="UTTAR PRADESH"
replace block = "PATTI" if block=="PATUTI" & district=="PRATAPGARH" & state=="UTTAR PRADESH"
replace block = "ROBERTSGANJ" if block=="ROBERTSGABH" & district=="SONBHADRA" & state=="UTTAR PRADESH"
replace block = "PINDRA" if block=="PINDARA" & district=="VARANASI" & state=="UTTAR PRADESH"
replace block = "PEDDAPALLE" if block=="PEDDAPALLI" & district=="KARIMNAGAR" & state=="ANDHRA PRADESH"
replace block = "TALLADA" if block=="THALLADA" & district=="KHAMMAM" & state=="ANDHRA PRADESH"
replace block = "BHOOTHPUR" if block=="BOOTHPUR" & district=="MAHBUBNAGAR" & state=="ANDHRA PRADESH"
replace block = "TADOOR" if block=="THADOOR" & district=="MAHBUBNAGAR" & state=="ANDHRA PRADESH"
replace block = "CHIVVEMLA" if block=="CHIVEMLA" & district=="NALGONDA" & state=="ANDHRA PRADESH"
replace block = "VALIGONDA" if block=="VOLIGONDA" & district=="NALGONDA" & state=="ANDHRA PRADESH"
replace block = "KURAVI" if block=="KORIVI" & district=="WARANGAL" & state=="ANDHRA PRADESH"
replace block = "DOHAD" if block=="DAHOD" & district=="DOHAD" & state=="GUJARAT"
replace block = "BANSDA" if block=="VANSDA" & district=="NAVSARI" & state=="GUJARAT"
replace block = "KADDAM (PEDDUR)" if block=="KADDAM" & district=="ADILABAD" & state=="ANDHRA PRADESH"
replace block = "CHILAKALURIPET H/O.PURUSHOTHA PATNAM" if block=="CHILAKALURIPET" & district=="GUNTUR" & state=="ANDHRA PRADESH"
replace block = "GUNTUR" if block=="GUNTUR RURAL" & district=="GUNTUR" & state=="ANDHRA PRADESH"
replace block = "SAVALYAPURAM H/O KANAMARLAPUDI" if block=="SAVALYAPURAM" & district=="GUNTUR" & state=="ANDHRA PRADESH"
replace block = "MADDIKERA (EAST)" if block=="MADDIKERA" & district=="KURNOOL" & state=="ANDHRA PRADESH"
replace block = "GHANPUR (STATION)" if block=="GHANPUR MULUG" & district=="WARANGAL" & state=="ANDHRA PRADESH"
replace block = "PATNA RURAL" if block=="PATNA RURAL (A) PATNA RURAL (B" & district=="PATNA" & state=="BIHAR"
replace block = "MANPUR" if block=="MANPUR (TD)" & district=="RAJNANDGAON" & state=="CHHATTISGARH"
replace block = "AMBIKAPUR" if block=="AMBIKAPUR (SARGUJA)" & district=="SURGUJA" & state=="CHHATTISGARH"
replace block = "DERA GOPIPUR" if block=="DERA" & district=="KANGRA" & state=="HIMACHAL PRADESH"
replace block = "CHANDUR RAILWAY" if block=="CHANDUR RLY" & district=="AMRAVATI" & state=="MAHARASHTRA"
replace block = "DHAMANGAON RAILWAY" if block=="DHAMANGAON RLY" & district=="AMRAVATI" & state=="MAHARASHTRA"
replace block = "DESAIGANJ (VADASA)" if block=="DESAIGANJ WADASA" & district=="GADCHIROLI" & state=="MAHARASHTRA"
replace block = "WOZHURO" if block=="WOZHURO RALAN" & district=="WOKHA" & state=="NAGALAND"
replace block = "BHADRAK RURAL" if block=="BHADRAK" & district=="BHADRAK" & state=="ORISSA"
replace block = "KISHANGARH BAS" if block=="KISHANGARH" & district=="ALWAR" & state=="RAJASTHAN"
replace block = "LALGANJ" if block=="LALGANJ AJHARA" & district=="PRATAPGARH" & state=="UTTAR PRADESH"
replace block = "TIRUPATI (RURAL)" if block=="TIRUPATI RURAL" & district=="CHITTOOR" & state=="ANDHRA PRADESH"
replace block = "TIRUPATI (URBAN)" if block=="TIRUPATI URBAN" & district=="CHITTOOR" & state=="ANDHRA PRADESH"
replace block = "KHAMMAM (RURAL)" if block=="KHAMMAM RURAL" & district=="KHAMMAM" & state=="ANDHRA PRADESH"
replace block = "KHAMMAM (URBAN)" if block=="KHAMMAM URBAN" & district=="KHAMMAM" & state=="ANDHRA PRADESH"
replace block = "SHANKARAMPET (A)" if block=="SHANKARAMPET A" & district=="MEDAK" & state=="ANDHRA PRADESH"
replace block = "JAISINGHPUR" if block=="JAI SINGH PUR" & district=="SULTANPUR" & state=="UTTAR PRADESH"
replace block = "VERNO" if block=="BHERNO" & district=="GUMLA" & state=="JHARKHAND"
replace block = "MARSAGHAI" if block=="MASHAGHAI" & district=="KENDRAPARA" & state=="ORISSA"
replace block = "UDAYPUR" if block=="UDAIYPUR" & district=="SURGUJA" & state=="CHHATTISGARH"
replace block = "PUSOUR" if block=="PUSORE" & district=="RAIGARH" & state=="CHHATTISGARH"
replace block = "BALODA" if block=="BALAUDA" & district=="JANJGIR - CHAMPA" & state=="CHHATTISGARH"
replace block = "CHHURIYA" if block=="CHHURIA" & district=="RAJNANDGAON" & state=="CHHATTISGARH"
replace block = "CHHURA" if block=="CHHURRA" & district=="RAIPUR" & state=="CHHATTISGARH"
replace block = "BAKAVAND" if block=="BAKAWAND" & district=="BASTAR" & state=="CHHATTISGARH"
*replace block = "MORENA" if block=="MORENA 1" & district=="MORENA" & state=="MADHYA PRADESH"
replace block = "LALBARRA" if block=="LALBURRA" & district=="BALAGHAT" & state=="MADHYA PRADESH"
replace block = "CHANDURBAZAR" if block=="CHANDUR BAZAR" & district=="AMRAVATI" & state=="MAHARASHTRA"
replace block = "ELGAID" if block=="ELIGAID" & district=="KARIMNAGAR" & state=="ANDHRA PRADESH"
replace block = "RAMPUR MANIHARAN" if block=="RAMPUR" & district=="SAHARANPUR" & state=="UTTAR PRADESH"
replace block = "BARI RAMACHANDRAPUR" if block=="BARI" & district=="JAJAPUR" & state=="ORISSA"
replace block = "DABRA (PICHHORE)" if block=="DABRA" & district=="GWALIOR" & state=="MADHYA PRADESH"
replace block = "GHANPUR (STATION)" if block=="GHANPUR STATION" & district=="WARANGAL" & state=="ANDHRA PRADESH"
replace block = "HARRAI" if block=="HARAI" & district=="CHHINDWARA" & state=="MADHYA PRADESH"
replace block = "SHAHPUR" if block=="SHAHAPUR" & district=="YADGIR" & state=="KARNATAKA"
replace block = "MARWA" if block=="MARWAH" & district=="DODA" & state=="JAMMU & KASHMIR"
replace block = "DHORIMANNA" if block=="DHORIMANA" & district=="BARMER" & state=="RAJASTHAN"
replace block = "SINDHARI" if block=="SINDHARY" & district=="BARMER" & state=="RAJASTHAN"
replace block = "BANKEYGANJ" if block=="BANKEGANJ" & district=="KHERI" & state=="UTTAR PRADESH"
replace block = "AILIYA" if block=="EALLIYA" & district=="SITAPUR" & state=="UTTAR PRADESH"
replace block = "MACHHREHTA" if block=="MACHREHTA" & district=="SITAPUR" & state=="UTTAR PRADESH"
replace block = "SAKRAN" if block=="SAKARAN" & district=="SITAPUR" & state=="UTTAR PRADESH"
replace block = "AHRORI" if block=="AHIROHI" & district=="HARDOI" & state=="UTTAR PRADESH"
*replace block = "BAWAN" if block=="BAABAN" & district=="HARDOI" & state=="UTTAR PRADESH"
replace block = "KOTHWAN" if block=="KATHWA" & district=="HARDOI" & state=="UTTAR PRADESH"
replace block = "BACHHRAWAN" if block=="BACHHRAON" & district=="RAE BARELI" & state=="UTTAR PRADESH"
replace block = "DIH" if block=="DIHA" & district=="RAE BARELI" & state=="UTTAR PRADESH"
*replace block = "KAMALGANJ" if block=="KAYAMGANJ" & district=="FARRUKHABAD" & state=="UTTAR PRADESH"
replace block = "BARHPURA" if block=="BADAPURA" & district=="ETAWAH" & state=="UTTAR PRADESH"
replace block = "MAHUVA" if block=="MAHUA" & district=="BANDA" & state=="UTTAR PRADESH"
replace block = "KARVI (CHITRAKOOT DHAM)" if block=="KARBI (CHITRAKOOT DHAM)" & district=="CHITRAKOOT" & state=="UTTAR PRADESH"
replace block = "DHATA" if block=="THATHA" & district=="FATEHPUR" & state=="UTTAR PRADESH"
replace block = "ASPUR DEOSARA" if block=="AASPUR DEVSARA" & district=="PRATAPGARH" & state=="UTTAR PRADESH"
replace block = "MANDHATA" if block=="MANDHYATHA" & district=="PRATAPGARH" & state=="UTTAR PRADESH"
replace block = "BAHRIA" if block=="BAHARIYA" & district=="ALLAHABAD" & state=="UTTAR PRADESH"
replace block = "DHANUPUR" if block=="DHANOOPUR" & district=="ALLAHABAD" & state=="UTTAR PRADESH"
replace block = "URUWAN" if block=="URUWA" & district=="ALLAHABAD" & state=="UTTAR PRADESH"
replace block = "TRIVEDIGANJ" if block=="TRIVENIGANJ" & district=="BARABANKI" & state=="UTTAR PRADESH"
replace block = "GESARI" if block=="GASARI" & district=="BALRAMPUR" & state=="UTTAR PRADESH"
replace block = "MUJHANA" if block=="MUJEHANA" & district=="GONDA" & state=="UTTAR PRADESH"
replace block = "PANDRI KRAPAL" if block=="PANDRI KRIPAL" & district=="GONDA" & state=="UTTAR PRADESH"
replace block = "VIKRAM JOT" if block=="VIKRAMJOT" & district=="BASTI" & state=="UTTAR PRADESH"
replace block = "SOTHA" if block=="SANTHA" & district=="SANT KABIR NAGAR" & state=="UTTAR PRADESH"
replace block = "PANIWARA" if block=="PANIYARA" & district=="MAHRAJGANJ" & state=="UTTAR PRADESH"
replace block = "GAUGAHA" if block=="GAGHA" & district=="GORAKHPUR" & state=="UTTAR PRADESH"
replace block = "FAZILNAGAR" if block=="FAJILNAGAR" & district=="KUSHINAGAR" & state=="UTTAR PRADESH"
replace block = "ATRAULIA" if block=="ATRAULIYA" & district=="AZAMGARH" & state=="UTTAR PRADESH"
replace block = "TARWA" if block=="TARWAN" & district=="AZAMGARH" & state=="UTTAR PRADESH"
replace block = "THEKMA" if block=="TEKMA" & district=="AZAMGARH" & state=="UTTAR PRADESH"
replace block = "FATEHPUR MADAUN" if block=="FATEHPUR MADRAUN" & district=="MAU" & state=="UTTAR PRADESH"
replace block = "VERUARWARI" if block=="BERUARWARI" & district=="BALLIA" & state=="UTTAR PRADESH"
replace block = "VARACHAKWAR" if block=="VARACHAVAR" & district=="GHAZIPUR" & state=="UTTAR PRADESH"
replace block = "CHAHNIYA" if block=="CHAHANIYA" & district=="CHANDAULI" & state=="UTTAR PRADESH"
replace block = "HALLIA" if block=="HALIYA" & district=="MIRZAPUR" & state=="UTTAR PRADESH"
replace block = "MARIHAN" if block=="MADIHAON" & district=="MIRZAPUR" & state=="UTTAR PRADESH"
replace block = "MYORPUR" if block=="MYOREPUR" & district=="SONBHADRA" & state=="UTTAR PRADESH"
replace block = "DAMBUK-PAGLAM" if block=="DUMBUK-PAGLAM" & district=="LOWER DIBANG VALLEY" & state=="ARUNACHAL PRADESH"
replace block = "DHAM DHAMA" if block=="DHAMDHAMA" & district=="NALBARI" & state=="ASSAM"
replace block = "MURKONGSELEK" if block=="MURKONSELEK" & district=="DHEMAJI" & state=="ASSAM"
replace block = "MAHUADANR" if block=="MAHUADAR" & district=="LATEHAR" & state=="JHARKHAND"
replace block = "BAMRA" if block=="BAMARA" & district=="SAMBALPUR" & state=="ORISSA"
replace block = "GUMA" if block=="GUMMA" & district=="GAJAPATI" & state=="ORISSA"
replace block = "BAUDH" if block=="BOUDH" & district=="BAUDH" & state=="ORISSA"
replace block = "BILHA" if block=="BELHA" & district=="BILASPUR" & state=="CHHATTISGARH"
replace block = "DURGKONDAL" if block=="DURGUKONDAL" & district=="KANKER" & state=="CHHATTISGARH"
replace block = "GEEDAM" if block=="GIDAM" & district=="DANTEWADA" & state=="CHHATTISGARH"
replace block = "KUWAKONDA" if block=="KUAKONDA" & district=="DANTEWADA" & state=="CHHATTISGARH"
replace block = "PAHADGARH" if block=="PAHARGARH" & district=="MORENA" & state=="MADHYA PRADESH"
replace block = "BHIMPURA" if block=="BHIMPUR" & district=="BETUL" & state=="MADHYA PRADESH"
replace block = "BAJANG" if block=="BAJAG" & district=="DINDORI" & state=="MADHYA PRADESH"
replace block = "MENHADWANI" if block=="MEHADWANI" & district=="DINDORI" & state=="MADHYA PRADESH"
replace block = "PUSHPARAJGARH" if block=="PUSHPRAJGARH" & district=="ANUPPUR" & state=="MADHYA PRADESH"
replace block = "BALUSSERI" if block=="BALUSSERY" & district=="KOZHIKODE" & state=="KERALA"
replace block = "KANDILI" if block=="KANDHILI" & district=="VELLORE" & state=="TAMIL NADU"
replace block = "TIMIRI" if block=="THIMIRI" & district=="VELLORE" & state=="TAMIL NADU"
replace block = "JAYANKONDAM" if block=="JAYAKOMDAM" & district=="ARIYALUR" & state=="TAMIL NADU"
replace block = "THIRUMANUR" if block=="TIRUMANUR" & district=="ARIYALUR" & state=="TAMIL NADU"
replace block = "VIJAYIPUR" if block=="BIJAIPUR" & district=="FATEHPUR" & state=="UTTAR PRADESH"
replace block = "KOT BHAI" if block=="KOT BHAI AT GIDDERBAHA" & district=="MUKTSAR" & state=="PUNJAB"
replace block = "KUCHAMAN CITY" if block=="KUCHAMAN" & district=="NAGAUR" & state=="RAJASTHAN"
replace block = "MAIRABARI PT." if block=="MAIRABARI (PART)" & district=="MARIGAON" & state=="ASSAM"
replace block = "CHOWKI" if block=="CHOWKI (TD)" & district=="RAJNANDGAON" & state=="CHHATTISGARH"
replace block = "KAIMGANJ" if block=="KAYAMGANJ" & district=="FARRUKHABAD" & state=="UTTAR PRADESH"
replace block = "KOYALIBEDA" if block=="KOILEBEDA" & district=="KANKER" & state=="CHHATTISGARH"
replace block = "THUAMUL RAMPUR" if block=="TH.RAMPUR" & district=="KALAHANDI" & state=="ORISSA"
replace block = "GHANSAUR" if block=="GHANSORE" & district=="SEONI" & state=="MADHYA PRADESH"
replace block = "MADANPUR RAMPUR" if block=="M.RAMPUR" & district=="KALAHANDI" & state=="ORISSA"
replace block = "BAIDHAN" if block=="WAIDHAN" & district=="SIDHI" & state=="MADHYA PRADESH"
replace block = "LACHHMANGARH" if block=="LAXMANGARH" & district=="ALWAR" & state=="RAJASTHAN"
replace block = "SATNA (SOHAWAL)" if block=="SOHAWAL" & district=="SATNA" & state=="MADHYA PRADESH"
replace block = "POUNDI-UPRODA" if block=="PODI UPARODA" & district=="KORBA" & state=="CHHATTISGARH"
replace block = "GIRD" if block=="MORAR" & district=="GWALIOR" & state=="MADHYA PRADESH"
replace block = "ADITYAPUR(GAMHARIA)" if block=="GAMHARIA" & district=="PASHCHIMI SINGHBHUM" & state=="JHARKHAND"
replace block = "MALHARGARH" if block=="MLAHARGRAH" & district=="MANDSAUR" & state=="MADHYA PRADESH"
replace block = "DONGARGARH" if block=="DONGERGARH" & district=="RAJNANDGAON" & state=="CHHATTISGARH"
replace block = "MOHLA" if block=="MOHALA (TD)" & district=="RAJNANDGAON" & state=="CHHATTISGARH"
replace block = "GOBINDPUR(RAJNAGAR)" if block=="RAJNAGAR" & district=="PASHCHIMI SINGHBHUM" & state=="JHARKHAND"
replace block = "BOLAGAD" if block=="BOLGARH" & district=="KHORDHA" & state=="ORISSA"
replace block = "BHANDARI POKHARI" if block=="BANDARIPOKHARI" & district=="BHADRAK" & state=="ORISSA"
replace block = "NARAIN BAGAR" if block=="NARYANBAGAR" & district=="CHAMOLI" & state=="UTTARAKHAND"
replace block = "BARSAHI" if block=="BADSAHI" & district=="MAYURBHANJ" & state=="ORISSA"
replace block = "KOULDHAR" if block=="THAULDHAR" & district=="TEHRI GARHWAL" & state=="UTTARAKHAND"
replace block = "KESHORAI PATAN" if block=="KESHORAIP" & district=="BUNDI" & state=="RAJASTHAN"
replace block = "BAHANAGA" if block=="BHANAGA" & district=="BALESHWAR" & state=="ORISSA"
replace block = "USUR" if block=="USOOR" & district=="DANTEWADA" & state=="CHHATTISGARH"
replace block = "ZAHRIKHAL" if block=="JAIRIKHAL" & district=="GARHWAL" & state=="UTTARAKHAND"
replace block = "BHOPALPATTNAM" if block=="BHOPAL PATNAM" & district=="DANTEWADA" & state=="CHHATTISGARH"
replace block = "KESHORAIPATAN" if block=="KESHORAIP" & district=="BUNDI" & state=="RAJASTHAN"
replace block = "ROING" if block=="ROING-KORONU" & district=="DIBANG VALLEY" & state=="ARUNACHAL PRADESH"
replace block = "BADAMBA" if block=="BARAMBA" & district=="CUTTACK" & state=="ORISSA"
replace block = "PALI NO.1" if block=="GOHPARU(PALI NO.1)" & district=="SHAHDOL" & state=="MADHYA PRADESH"
replace block = "BARAILY" if block=="BARELI (BADI)" & district=="RAISEN" & state=="MADHYA PRADESH"
replace block = "HOSHIARPUR I" if block=="HOSHIARPUR 1" & district=="HOSHIARPUR" & state=="PUNJAB"
replace block = "KARNAPRAYAG" if block=="KARANPRYAG" & district=="CHAMOLI" & state=="UTTARAKHAND"
replace block = "ORCHHA" if block=="ORCHHA(ABHUJMAD)" & district=="BASTAR" & state=="CHHATTISGARH"
replace block = "DONGARGAON" if block=="DONGERGAON" & district=="RAJNANDGAON" & state=="CHHATTISGARH"
replace block = "KHADGANVA" if block=="KHADGAWANA" & district=="KORIYA" & state=="CHHATTISGARH"
replace block = "GADARWARA" if block=="SAINKHEDA (GADARWAD)" & district=="NARSIMHAPUR" & state=="MADHYA PRADESH"
replace block = "BAYTOO" if block=="BAITU" & district=="BARMER" & state=="RAJASTHAN"
replace block = "UMARIA(KARKELI)" if block=="KARKELI" & district=="UMARIA" & state=="MADHYA PRADESH"
replace block = "SUPA" if block=="JOIDA (SUPA)" & district=="UTTARA KANNADA" & state=="KARNATAKA"
replace block = "HAWAI" if block=="HAWAI-WALONG" & district=="" & state=="ARUNACHAL PRADESH"
replace block = "NANDGAON-KHANDESHWAR" if block=="NANDGAON KH." & district=="AMRAVATI" & state=="MAHARASHTRA"
replace block = "GOGAON" if block=="GOGANWA" & district=="WEST NIMAR" & state=="MADHYA PRADESH"
replace block = "SIDLI-CHIRANG PT." if block=="SIDLI CHIRANG" & district=="BONGAIGAON" & state=="ASSAM"
replace block = "BHARAWAN" if block=="BHARAVNA" & district=="HARDOI" & state=="UTTAR PRADESH"
replace block = "NUAON" if block=="NOAWAN" & district=="KAIMUR (BHABUA)" & state=="BIHAR"
replace block = "SIDLI-CHIRANG PT." if block=="SIDLI CHIRANG" & district=="KOKRAJHAR" & state=="ASSAM"
replace block = "KEOTIRANWAY" if block=="KEOTI" & district=="DARBHANGA" & state=="BIHAR"
replace block = "RAGA" if block=="TAMEN-RAGA" & district=="LOWER SUBANSIRI" & state=="ARUNACHAL PRADESH"
replace block = "NUAGAON" if block=="K.NUAGAN" & district=="KANDHAMAL" & state=="ORISSA"
replace block = "BANEIGARH" if block=="BONEIGARH" & district=="SUNDARGARH" & state=="ORISSA"
replace block = "AIRAYA" if block=="ARAIN" & district=="FATEHPUR" & state=="UTTAR PRADESH"
replace block = "KOLORIANG" if block=="KOLORIANG-SARLI" & district=="LOWER SUBANSIRI" & state=="ARUNACHAL PRADESH"
replace block = "HAWAI" if block=="HAWAI-WALONG" & district=="LOHIT" & state=="ARUNACHAL PRADESH"
replace block = "DETROJ-RAMPURA" if block=="DETROJ RANPUR" & district=="AHMADABAD" & state=="GUJARAT"
replace block = "DANGTOLA" if block=="DANGTOL" & district=="BONGAIGAON" & state=="ASSAM"
}




replace village = upper(trim(itrim(village)))
replace village = "-" if village==""
gen APvi_code = real(substr(village,-3,2)) if state=="ANDHRA PRADESH" & substr(village,-4,1)=="(" & substr(village,-1,1)==")"
replace village = substr(village,1,length(village)-4) if state=="ANDHRA PRADESH" & APvi_code!=.
replace village = upper(trim(itrim(village)))

gen st_code=.
replace st_code = 28 if state=="ANDHRA PRADESH"
replace st_code = 12 if state=="ARUNACHAL PRADESH"
replace st_code = 18 if state=="ASSAM"
replace st_code = 10 if state=="BIHAR"
replace st_code = 22 if state=="CHHATTISGARH"
replace st_code = 24 if state=="GUJARAT"
replace st_code = 6  if state=="HARYANA"
replace st_code = 2  if state=="HIMACHAL PRADESH"
replace st_code = 1  if state=="JAMMU & KASHMIR"
replace st_code = 20 if state=="JHARKHAND"
replace st_code = 29 if state=="KARNATAKA"
replace st_code = 32 if state=="KERALA"
replace st_code = 23 if state=="MADHYA PRADESH"
replace st_code = 27 if state=="MAHARASHTRA"
replace st_code = 14 if state=="MANIPUR"
replace st_code = 17 if state=="MEGHALAYA"
replace st_code = 15 if state=="MIZORAM"
replace st_code = 13 if state=="NAGALAND"
replace st_code = 21 if state=="ORISSA"
replace st_code = 3  if state=="PUNJAB"
replace st_code = 8  if state=="RAJASTHAN"
replace st_code = 11 if state=="SIKKIM"
replace st_code = 33 if state=="TAMIL NADU"
replace st_code = 16 if state=="TRIPURA"
replace st_code = 9  if state=="UTTAR PRADESH"
replace st_code = 5  if state=="UTTARAKHAND"
replace st_code = 19 if state=="WEST BENGAL"
*replace st_code = 4  if state=="CHANDIGARH"
*replace st_code = 7  if state=="DEHLI"
*replace st_code = 25 if state=="DAMAN AND DIU"
*replace st_code = 26 if state=="DADRA AND NAGAR HAVELI"
*replace st_code = 30 if state=="GOA"
*replace st_code = 31 if state=="LAKSHDWEEP"
*replace st_code = 34 if state=="PONDICHERRY"
*replace st_code = 35 if state=="ANDAMAN AND NICOBAR ISLANDS"
drop if st_code ==.
sort st_code district
duplicates drop
gen h9_id = _n
order state district block village habitation
egen h9v_id = group(st_code state district block village)
assert h9v_id !=.
destring totp scp stp, replace force
compress
save "$hab9/hab_census09_for_merge_all.dta", replace
collapse (sum) totp_h9=totp (max) maxp_h9=totp (count) count_h9=h9_id, by(st_code state district block village h9v_id)
compress
save "$hab9/hab_census09_for_merge.dta", replace
}

********************************************************************************
********************************************************************************

** Step 8: Make sure all states & districts have exact matches, copy dt_codes
{
use "$hab9/hab_census09_for_merge.dta", clear
keep state st_code district 
duplicates drop
merge 1:m state st_code district using "$hmerge/pca_2001_names_merges_all.dta", gen(merge_dt)  // try PCA 2001 district name
keep state st_code district dt_code merge_dt
duplicates drop 
sort state district
drop if merge_dt==2
merge 1:m state st_code district using "$hab9/hab_census09_for_merge.dta", nogen
order st_code state dt_code district merge_dt
sort  st_code state dt_code district merge_dt
assert dt_code!=. & dt_code>0 if merge_dt==3
save "$hab9/hab_census09_for_merge.dta", replace

keep state st_code district // try PCA 2011 district name
duplicates drop
rename district district11
merge 1:m state st_code district11 using "$hmerge/pca_2001_names_merges_all.dta", gen(merge_dt11) keepusing(dt_code11) 
duplicates drop
drop if merge_dt11==2
rename district11 district
duplicates drop
merge 1:m state st_code district using "$hab9/hab_census09_for_merge.dta", nogen
duplicates drop
save "$hab9/hab_census09_for_merge.dta", replace

keep state st_code district dt_code merge_dt merge_dt11 // try RGGVY district name
duplicates drop
rename district district_rggvy
rename dt_code dt_code_hab
merge 1:m state st_code district_rggvy using "$hmerge/pca_2001_names_merges_all.dta", gen(merge_dtrggvy) keepusing(district dt_code) 
duplicates drop
replace dt_code_hab = dt_code if dt_code_hab==. & merge_dtrggvy==3
drop if merge_dtrggvy==2
drop district dt_code
rename district_rggvy district
duplicates drop
merge 1:m state st_code district using "$hab9/hab_census09_for_merge.dta", nogen
replace dt_code = dt_code_hab if dt_code==. & dt_code_hab!=.
drop dt_code_hab
duplicates drop
save "$hab9/hab_census09_for_merge.dta", replace

keep state st_code district dt_code dt_code11 merge_dt merge_dt11 merge_dtrggvy // try RGGVY habitation list district name
duplicates drop
rename district district_lh
rename dt_code dt_code_hab
merge 1:m state st_code district_lh using "$hmerge/pca_2001_names_merges_all.dta", gen(merge_dtlh) keepusing(district dt_code) 
duplicates drop
replace dt_code_hab = dt_code if dt_code_hab==. & merge_dtlh==3 & (district_lh!="CHIRANG" & district_lh!="UDALGURI" & district_lh!="NARAYANPUR" & district_lh!="BAKSA")
drop if merge_dtlh==2
drop district dt_code
rename district_lh district
duplicates drop
merge 1:m state st_code district using "$hab9/hab_census09_for_merge.dta", nogen
replace dt_code = dt_code_hab if dt_code==. & dt_code_hab!=.
drop dt_code_hab
duplicates drop
sort st_code dt_code h9v_id
order state st_code district dt_code block village 
save "$hab9/hab_census09_for_merge.dta", replace
}

********************************************************************************
********************************************************************************

** Step 9: Make sure all states & districts & blocks have exact matches, copy bk_codes
{
use "$hab9/hab_census09_for_merge.dta", clear
keep state st_code district dt_code block merge*
duplicates drop
merge 1:m state st_code district dt_code block using "$hmerge/pca_2001_names_merges_all.dta", gen(merge_bk)
drop if (district=="" | block=="") & merge_bk==2
keep state st_code district dt_code block bk_code_pca merge*
duplicates drop 
sort state district block
/*
keep if merge_bk<3 
gen block_name_merge = ""  // create field for merge name, to go back and replace when insheeting habitation census
gen q = `"""'

*Iterate 2x to correct blocks that are 1-2 letters off
gen block_pca_above = block[_n-1] if merge_bk[_n-1]==2
gen block_pca_below = block[_n+1] if merge_bk[_n+1]==2
strdist block block_pca_above if merge_bk==1, gen(dist_above)
strdist block block_pca_below if merge_bk==1, gen(dist_below)
br block block_pca_above dist_above if dist_above<3
br block block_pca_below dist_below if dist_below<3
replace block_name_merge = block_pca_above if merge_bk==1 & merge_bk[_n-1]==2 & dist_above<3 & dist_below>3 & state==state[_n-1] & district==district[_n-1] & merge_dt==3 
replace block_name_merge = block_pca_below if merge_bk==1 & merge_bk[_n+1]==2 & dist_below<3 & dist_above>3 & state==state[_n+1] & district==district[_n+1] & merge_dt==3 
gen replace_text = "replace block = " + q + block_name_merge + q + " if block==" + q + block + q + " & district==" + q + district + q + " & state==" + q + state + q if block_name_merge!="" & word(block_name_merge,-1)!="I" & word(block_name_merge,-1)!="II" & word(block_name_merge,-1)!="III" & word(block_name_merge,-1)!="1" & word(block_name_merge,-1)!="2" & word(block,-1)!="I" & word(block,-1)!="II" & word(block,-1)!="III" & word(block,-1)!="1" & word(block,-1)!="2"
br if replace_text!=""

*Iterate 1x to correct blocks that have a matching word
gen block_pca_above = block[_n-1] if merge_bk[_n-1]==2
gen block_pca_below = block[_n+1] if merge_bk[_n+1]==2
gen block_name_merge_above = block_pca_above if merge_bk==1 & merge_bk[_n-1]==2 & word(block,1)==word(block_pca_above,1) & state==state[_n-1] & district==district[_n-1] & merge_dt==3 
gen block_name_merge_below = block_pca_below if merge_bk==1 & merge_bk[_n+1]==2 & word(block,1)==word(block_pca_below,1) & state==state[_n+1] & district==district[_n+1] & merge_dt==3 
br state district block block_pca* block_name_merge_* if block_name_merge_above!="" | block_name_merge_below!=""
replace block_name_merge = block_name_merge_above if block_name_merge=="" & word(block_name_merge_above,-1)!="I" & word(block_name_merge_above,-1)!="II" & word(block_name_merge_above,-1)!="III" & word(block_name_merge_above,-1)!="WEST" & word(block_name_merge_above,-1)!="EAST" & word(block_name_merge_above,-1)!="SOUTH" & word(block_name_merge_above,-1)!="NORTH" & word(block,-1)!="I" & word(block,-1)!="II" & word(block,-1)!="III" & word(block,-1)!="WEST" & word(block,-1)!="EAST" & word(block,-1)!="SOUTH" & word(block,-1)!="NORTH"  
replace block_name_merge = block_name_merge_below if block_name_merge=="" & word(block_name_merge_below,-1)!="I" & word(block_name_merge_below,-1)!="II" & word(block_name_merge_below,-1)!="III" & word(block_name_merge_below,-1)!="WEST" & word(block_name_merge_below,-1)!="EAST" & word(block_name_merge_below,-1)!="SOUTH" & word(block_name_merge_below,-1)!="NORTH" & word(block,-1)!="I" & word(block,-1)!="II" & word(block,-1)!="III" & word(block,-1)!="WEST" & word(block,-1)!="EAST" & word(block,-1)!="SOUTH" & word(block,-1)!="NORTH"  
gen replace_text = "replace block = " + q + block_name_merge + q + " if block==" + q + block + q + " & district==" + q + district + q + " & state==" + q + state + q if block_name_merge!="" & word(block_name_merge,-1)!="I" & word(block_name_merge,-1)!="II" & word(block_name_merge,-1)!="III" & word(block,-1)!="I" & word(block,-1)!="II" & word(block,-1)!="III"
br if replace_text!=""
*/
drop if merge_bk==2
replace bk_code = . if block=="SIRPUR" & district=="ADILABAD" & state=="ANDHRA PRADESH"
duplicates drop
merge 1:m state st_code district dt_code block using "$hab9/hab_census09_for_merge.dta", nogen
order st_code state dt_code dt_code11 district block bk_code_pca
sort  st_code state dt_code dt_code11 district block bk_code_pca
save "$hab9/hab_census09_for_merge.dta", replace



use "$hab9/hab_census09_for_merge.dta", clear // try PCA 2011 block names
keep st_code state dt_code dt_code11 district block merge*
rename block block11
rename district district11
duplicates drop
merge 1:m st_code district11 dt_code11 block11 using "$hmerge/pca_2001_names_merges_all.dta", gen(merge_bk11)
keep st_code state dt_code dt_code11 district11 block11 bk_code11 merge_bk merge_bk11
sort st_code dt_code block11
replace state = state[_n-1] if state=="" & st_code==st_code[_n-1]
replace state = state[_n+1] if state=="" & st_code==st_code[_n+1]
replace district = district[_n-1] if district=="" & dt_code11==dt_code11[_n-1]
replace district = district[_n+1] if district=="" & dt_code11==dt_code11[_n+1]
duplicates drop 
/*
keep if merge_bk11<3 & merge_bk!=3
drop if block11==""
gen block_name_merge = ""  // create field for merge name, to go back and replace when insheeting habitation census
gen q = `"""'

*Iterate 1x to correct blocks that are 1-2 letters off
gen block_pca_above = block11[_n-1] if merge_bk11[_n-1]==2
gen block_pca_below = block11[_n+1] if merge_bk11[_n+1]==2
strdist block11 block_pca_above if merge_bk11==1, gen(dist_above)
strdist block11 block_pca_below if merge_bk11==1, gen(dist_below)
br block11 block_pca_above dist_above if dist_above<3
br block11 block_pca_below dist_below if dist_below<3
replace block_name_merge = block_pca_above if merge_bk11==1 & merge_bk11[_n-1]==2 & dist_above<3 & dist_below>3 & state==state[_n-1] & district==district[_n-1] 
replace block_name_merge = block_pca_below if merge_bk11==1 & merge_bk11[_n+1]==2 & dist_below<3 & dist_above>3 & state==state[_n+1] & district==district[_n+1] 
gen replace_text = "replace block = " + q + block_name_merge + q + " if block==" + q + block11 + q + " & district==" + q + district + q + " & state==" + q + state + q if block_name_merge!="" & word(block_name_merge,-1)!="I" & word(block_name_merge,-1)!="II" & word(block_name_merge,-1)!="III" & word(block11,-1)!="I" & word(block11,-1)!="II" & word(block11,-1)!="III"
br if replace_text!=""

*Iterate 1x to correct blocks that have a matching word
gen block_pca_above = block11[_n-1] if merge_bk11[_n-1]==2
gen block_pca_below = block11[_n+1] if merge_bk11[_n+1]==2
gen block_name_merge_above = block_pca_above if merge_bk11==1 & merge_bk11[_n-1]==2 & word(block11,1)==word(block_pca_above,1) & state==state[_n-1] & district==district[_n-1] 
gen block_name_merge_below = block_pca_below if merge_bk11==1 & merge_bk11[_n+1]==2 & word(block11,1)==word(block_pca_below,1) & state==state[_n+1] & district==district[_n+1] 
br state district block11 block_pca* block_name_merge_* if block_name_merge_above!="" | block_name_merge_below!=""
replace block_name_merge = block_name_merge_above if block_name_merge=="" & word(block_name_merge_above,-1)!="I" & word(block_name_merge_above,-1)!="II" & word(block_name_merge_above,-1)!="III" & word(block_name_merge_above,-1)!="WEST" & word(block_name_merge_above,-1)!="EAST" & word(block_name_merge_above,-1)!="SOUTH" & word(block_name_merge_above,-1)!="NORTH" & word(block11,-1)!="I" & word(block11,-1)!="II" & word(block11,-1)!="III" & word(block11,-1)!="WEST" & word(block11,-1)!="EAST" & word(block11,-1)!="SOUTH" & word(block11,-1)!="NORTH"  
replace block_name_merge = block_name_merge_below if block_name_merge=="" & word(block_name_merge_below,-1)!="I" & word(block_name_merge_below,-1)!="II" & word(block_name_merge_below,-1)!="III" & word(block_name_merge_below,-1)!="WEST" & word(block_name_merge_below,-1)!="EAST" & word(block_name_merge_below,-1)!="SOUTH" & word(block_name_merge_below,-1)!="NORTH" & word(block11,-1)!="I" & word(block11,-1)!="II" & word(block11,-1)!="III" & word(block11,-1)!="WEST" & word(block11,-1)!="EAST" & word(block11,-1)!="SOUTH" & word(block11,-1)!="NORTH"  
gen replace_text = "replace block = " + q + block_name_merge + q + " if block==" + q + block11 + q + " & district==" + q + district + q + " & state==" + q + state + q if block_name_merge!="" & word(block_name_merge,-1)!="I" & word(block_name_merge,-1)!="II" & word(block_name_merge,-1)!="III" & word(block11,-1)!="I" & word(block11,-1)!="II" & word(block11,-1)!="III"
br if replace_text!=""
*/
drop if merge_bk11==2
duplicates drop
rename block11 block
rename district11 district
replace bk_code11 = . if block=="SIRPUR" & district=="ADILABAD" & state=="ANDHRA PRADESH"
merge m:m state st_code district dt_code11 block using "$hab9/hab_census09_for_merge.dta", nogen
order st_code state dt_code dt_code11 district block bk_code_pca bk_code11
sort  st_code state dt_code dt_code11 district block bk_code_pca bk_code11
save "$hab9/hab_census09_for_merge.dta", replace



use "$hab9/hab_census09_for_merge.dta", clear // try Concordance 2001 block names
keep st_code state dt_code district block merge*
rename block block_2001
duplicates drop
merge 1:m st_code dt_code district block_2001 using "$hmerge/pca_2001_names_merges_all.dta", gen(merge_bkconc01)
keep st_code state dt_code district block_2001 bk_code_conc01 merge_bk merge_bk11 merge_bkconc01
sort st_code dt_code block_2001
replace state = state[_n-1] if state=="" & st_code==st_code[_n-1]
replace state = state[_n+1] if state=="" & st_code==st_code[_n+1]
replace district = district[_n-1] if district=="" & dt_code==dt_code[_n-1]
replace district = district[_n+1] if district=="" & dt_code==dt_code[_n+1]
duplicates drop 
/*
keep if merge_bkconc01<3 & merge_bk!=3 & merge_bk11!=3
drop if block_2001==""
gen block_name_merge = ""  // create field for merge name, to go back and replace when insheeting habitation census
gen q = `"""'

*Iterate 1x to correct blocks that are 1-2 letters off
gen block_pca_above = block_2001[_n-1] if merge_bkconc01[_n-1]==2
gen block_pca_below = block_2001[_n+1] if merge_bkconc01[_n+1]==2
strdist block_2001 block_pca_above if merge_bkconc01==1, gen(dist_above)
strdist block_2001 block_pca_below if merge_bkconc01==1, gen(dist_below)
br block_2001 block_pca_above dist_above if dist_above<3
br block_2001 block_pca_below dist_below if dist_below<3
replace block_name_merge = block_pca_above if merge_bkconc01==1 & merge_bkconc01[_n-1]==2 & dist_above<3 & dist_below>3 & state==state[_n-1] & district==district[_n-1] 
replace block_name_merge = block_pca_below if merge_bkconc01==1 & merge_bkconc01[_n+1]==2 & dist_below<3 & dist_above>3 & state==state[_n+1] & district==district[_n+1] 
gen replace_text = "replace block = " + q + block_name_merge + q + " if block==" + q + block_2001 + q + " & district==" + q + district + q + " & state==" + q + state + q if block_name_merge!="" & word(block_name_merge,-1)!="I" & word(block_name_merge,-1)!="II" & word(block_name_merge,-1)!="III" & word(block_2001,-1)!="I" & word(block_2001,-1)!="II" & word(block_2001,-1)!="III"
br if replace_text!=""

*Iterate 0x to correct blocks that have a matching word
gen block_pca_above = block_2001[_n-1] if merge_bkconc01[_n-1]==2
gen block_pca_below = block_2001[_n+1] if merge_bkconc01[_n+1]==2
gen block_name_merge_above = block_pca_above if merge_bkconc01==1 & merge_bkconc01[_n-1]==2 & word(block_2001,1)==word(block_pca_above,1) & state==state[_n-1] & district==district[_n-1] 
gen block_name_merge_below = block_pca_below if merge_bkconc01==1 & merge_bkconc01[_n+1]==2 & word(block_2001,1)==word(block_pca_below,1) & state==state[_n+1] & district==district[_n+1] 
br state district block_2001 block_pca* block_name_merge_* if block_name_merge_above!="" | block_name_merge_below!=""
replace block_name_merge = block_name_merge_above if block_name_merge=="" & word(block_name_merge_above,-1)!="I" & word(block_name_merge_above,-1)!="II" & word(block_name_merge_above,-1)!="III" & word(block_name_merge_above,-1)!="WEST" & word(block_name_merge_above,-1)!="EAST" & word(block_name_merge_above,-1)!="SOUTH" & word(block_name_merge_above,-1)!="NORTH" & word(block_2001,-1)!="I" & word(block_2001,-1)!="II" & word(block_2001,-1)!="III" & word(block_2001,-1)!="WEST" & word(block_2001,-1)!="EAST" & word(block_2001,-1)!="SOUTH" & word(block_2001,-1)!="NORTH"  
replace block_name_merge = block_name_merge_below if block_name_merge=="" & word(block_name_merge_below,-1)!="I" & word(block_name_merge_below,-1)!="II" & word(block_name_merge_below,-1)!="III" & word(block_name_merge_below,-1)!="WEST" & word(block_name_merge_below,-1)!="EAST" & word(block_name_merge_below,-1)!="SOUTH" & word(block_name_merge_below,-1)!="NORTH" & word(block_2001,-1)!="I" & word(block_2001,-1)!="II" & word(block_2001,-1)!="III" & word(block_2001,-1)!="WEST" & word(block_2001,-1)!="EAST" & word(block_2001,-1)!="SOUTH" & word(block_2001,-1)!="NORTH"  
gen replace_text = "replace block = " + q + block_name_merge + q + " if block==" + q + block_2001 + q + " & district==" + q + district + q + " & state==" + q + state + q if block_name_merge!="" & word(block_name_merge,-1)!="I" & word(block_name_merge,-1)!="II" & word(block_name_merge,-1)!="III" & word(block_2001,-1)!="I" & word(block_2001,-1)!="II" & word(block_2001,-1)!="III"
br if replace_text!=""
*/
drop if merge_bkconc01==2
duplicates drop
rename block_2001 block
replace bk_code_conc01 = . if block=="SIRPUR" & district=="ADILABAD" & state=="ANDHRA PRADESH"
merge m:m state st_code district dt_code block using "$hab9/hab_census09_for_merge.dta", nogen
order st_code state dt_code dt_code11 district block bk_code_pca bk_code11 bk_code_conc01
sort  st_code state dt_code dt_code11 district block bk_code_pca bk_code11 bk_code_conc01
save "$hab9/hab_census09_for_merge.dta", replace



use "$hab9/hab_census09_for_merge.dta", clear // try Concordance 2011 block names
keep st_code state dt_code dt_code11 district block merge*
rename block block_2011
rename district district11
duplicates drop
merge 1:m st_code state dt_code11 district11 block_2011 using "$hmerge/pca_2001_names_merges_all.dta", gen(merge_bkconc11)
keep st_code state dt_code dt_code11 district11 block_2011 bk_code_conc11 merge_bk merge_bk11 merge_bkconc01 merge_bkconc11
sort st_code dt_code11 block_2011
replace state = state[_n-1] if state=="" & st_code==st_code[_n-1]
replace state = state[_n+1] if state=="" & st_code==st_code[_n+1]
replace district = district[_n-1] if district=="" & dt_code==dt_code11[_n-1]
replace district = district[_n+1] if district=="" & dt_code==dt_code11[_n+1]
duplicates drop 
/*
keep if merge_bkconc11<3 & merge_bk!=3 & merge_bk11!=3 & merge_bkconc01!=3
drop if block_2011==""
gen block_name_merge = ""  // create field for merge name, to go back and replace when insheeting habitation census
gen q = `"""'

*Iterate 1x to correct blocks that are 1-2 letters off
gen block_pca_above = block_2011[_n-1] if merge_bkconc11[_n-1]==2
gen block_pca_below = block_2011[_n+1] if merge_bkconc11[_n+1]==2
strdist block_2011 block_pca_above if merge_bkconc11==1, gen(dist_above)
strdist block_2011 block_pca_below if merge_bkconc11==1, gen(dist_below)
br block_2011 block_pca_above dist_above if dist_above<3
br block_2011 block_pca_below dist_below if dist_below<3
replace block_name_merge = block_pca_above if merge_bkconc11==1 & merge_bkconc11[_n-1]==2 & dist_above<3 & dist_below>3 & state==state[_n-1] & district==district[_n-1] 
replace block_name_merge = block_pca_below if merge_bkconc11==1 & merge_bkconc11[_n+1]==2 & dist_below<3 & dist_above>3 & state==state[_n+1] & district==district[_n+1] 
gen replace_text = "replace block = " + q + block_name_merge + q + " if block==" + q + block_2011 + q + " & district==" + q + district + q + " & state==" + q + state + q if block_name_merge!="" & word(block_name_merge,-1)!="I" & word(block_name_merge,-1)!="II" & word(block_name_merge,-1)!="III" & word(block_2011,-1)!="I" & word(block_2011,-1)!="II" & word(block_2011,-1)!="III"
br if replace_text!=""

*Iterate 1x to correct blocks that have a matching word
gen block_pca_above = block_2011[_n-1] if merge_bkconc11[_n-1]==2
gen block_pca_below = block_2011[_n+1] if merge_bkconc11[_n+1]==2
gen block_name_merge_above = block_pca_above if merge_bkconc11==1 & merge_bkconc11[_n-1]==2 & word(block_2011,1)==word(block_pca_above,1) & state==state[_n-1] & district==district[_n-1] 
gen block_name_merge_below = block_pca_below if merge_bkconc11==1 & merge_bkconc11[_n+1]==2 & word(block_2011,1)==word(block_pca_below,1) & state==state[_n+1] & district==district[_n+1] 
br state district block_2011 block_pca* block_name_merge_* if block_name_merge_above!="" | block_name_merge_below!=""
replace block_name_merge = block_name_merge_above if block_name_merge=="" & word(block_name_merge_above,-1)!="I" & word(block_name_merge_above,-1)!="II" & word(block_name_merge_above,-1)!="III" & word(block_name_merge_above,-1)!="WEST" & word(block_name_merge_above,-1)!="EAST" & word(block_name_merge_above,-1)!="SOUTH" & word(block_name_merge_above,-1)!="NORTH" & word(block_2011,-1)!="I" & word(block_2011,-1)!="II" & word(block_2011,-1)!="III" & word(block_2011,-1)!="WEST" & word(block_2011,-1)!="EAST" & word(block_2011,-1)!="SOUTH" & word(block_2011,-1)!="NORTH"  
replace block_name_merge = block_name_merge_below if block_name_merge=="" & word(block_name_merge_below,-1)!="I" & word(block_name_merge_below,-1)!="II" & word(block_name_merge_below,-1)!="III" & word(block_name_merge_below,-1)!="WEST" & word(block_name_merge_below,-1)!="EAST" & word(block_name_merge_below,-1)!="SOUTH" & word(block_name_merge_below,-1)!="NORTH" & word(block_2011,-1)!="I" & word(block_2011,-1)!="II" & word(block_2011,-1)!="III" & word(block_2011,-1)!="WEST" & word(block_2011,-1)!="EAST" & word(block_2011,-1)!="SOUTH" & word(block_2011,-1)!="NORTH"  
gen replace_text = "replace block = " + q + block_name_merge + q + " if block==" + q + block_2011 + q + " & district==" + q + district + q + " & state==" + q + state + q if block_name_merge!="" & word(block_name_merge,-1)!="I" & word(block_name_merge,-1)!="II" & word(block_name_merge,-1)!="III" & word(block_2011,-1)!="I" & word(block_2011,-1)!="II" & word(block_2011,-1)!="III"
br if replace_text!=""
*/
drop if merge_bkconc11==2
duplicates drop
rename block_2011 block
rename district11 district
replace bk_code_conc11 = . if block=="SIRPUR" & district=="ADILABAD" & state=="ANDHRA PRADESH"
merge m:m state st_code district dt_code11 block using "$hab9/hab_census09_for_merge.dta", nogen
order st_code state dt_code dt_code11 district block bk_code_pca bk_code11 bk_code_conc01 bk_code_conc11
sort  st_code state dt_code dt_code11 district block bk_code_pca bk_code11 bk_code_conc01 bk_code_conc11
save "$hab9/hab_census09_for_merge.dta", replace



use "$hab9/hab_census09_for_merge.dta", clear // try RGGVY block names
keep st_code state dt_code district block merge*
rename block block_rggvy
duplicates drop
merge 1:m st_code state dt_code district block_rggvy using "$hmerge/pca_2001_names_merges_all.dta", gen(merge_bkrggvy)
keep st_code state dt_code district block_rggvy bk_code_rggvy merge_bk merge_bk11 merge_bkconc01 merge_bkconc11 merge_bkrggvy
sort st_code dt_code block_rggvy
replace state = state[_n-1] if state=="" & st_code==st_code[_n-1]
replace state = state[_n+1] if state=="" & st_code==st_code[_n+1]
replace district = district[_n-1] if district=="" & dt_code==dt_code[_n-1]
replace district = district[_n+1] if district=="" & dt_code==dt_code[_n+1]
duplicates drop 
/*
keep if merge_bkrggvy<3 & merge_bk!=3 & merge_bk11!=3 & merge_bkconc01!=3 & merge_bkconc11!=3
drop if block_rggvy==""
gen block_name_merge = ""  // create field for merge name, to go back and replace when insheeting habitation census
gen q = `"""'

*Iterate 2x to correct blocks that are 1-2 letters off
gen block_pca_above = block_rggvy[_n-1] if merge_bkrggvy[_n-1]==2
gen block_pca_below = block_rggvy[_n+1] if merge_bkrggvy[_n+1]==2
strdist block_rggvy block_pca_above if merge_bkrggvy==1, gen(dist_above)
strdist block_rggvy block_pca_below if merge_bkrggvy==1, gen(dist_below)
br block_rggvy block_pca_above dist_above if dist_above<3
br block_rggvy block_pca_below dist_below if dist_below<3
replace block_name_merge = block_pca_above if merge_bkrggvy==1 & merge_bkrggvy[_n-1]==2 & dist_above<3 & dist_below>3 & state==state[_n-1] & district==district[_n-1] 
replace block_name_merge = block_pca_below if merge_bkrggvy==1 & merge_bkrggvy[_n+1]==2 & dist_below<3 & dist_above>3 & state==state[_n+1] & district==district[_n+1] 
gen replace_text = "replace block = " + q + block_name_merge + q + " if block==" + q + block_rggvy + q + " & district==" + q + district + q + " & state==" + q + state + q if block_name_merge!="" & word(block_name_merge,-1)!="I" & word(block_name_merge,-1)!="II" & word(block_name_merge,-1)!="III" & word(block_rggvy,-1)!="I" & word(block_rggvy,-1)!="II" & word(block_rggvy,-1)!="III"
br if replace_text!="" & block_name_merge!=block_rggvy

*Iterate 1x to correct blocks that have a matching word
gen block_pca_above = block_rggvy[_n-1] if merge_bkrggvy[_n-1]==2
gen block_pca_below = block_rggvy[_n+1] if merge_bkrggvy[_n+1]==2
gen block_name_merge_above = block_pca_above if merge_bkrggvy==1 & merge_bkrggvy[_n-1]==2 & word(block_rggvy,1)==word(block_pca_above,1) & state==state[_n-1] & district==district[_n-1] 
gen block_name_merge_below = block_pca_below if merge_bkrggvy==1 & merge_bkrggvy[_n+1]==2 & word(block_rggvy,1)==word(block_pca_below,1) & state==state[_n+1] & district==district[_n+1] 
br state district block_rggvy block_pca* block_name_merge_* if block_name_merge_above!="" | block_name_merge_below!=""
replace block_name_merge = block_name_merge_above if block_name_merge=="" & word(block_name_merge_above,-1)!="I" & word(block_name_merge_above,-1)!="II" & word(block_name_merge_above,-1)!="III" & word(block_name_merge_above,-1)!="WEST" & word(block_name_merge_above,-1)!="EAST" & word(block_name_merge_above,-1)!="SOUTH" & word(block_name_merge_above,-1)!="NORTH" & word(block_rggvy,-1)!="I" & word(block_rggvy,-1)!="II" & word(block_rggvy,-1)!="III" & word(block_rggvy,-1)!="WEST" & word(block_rggvy,-1)!="EAST" & word(block_rggvy,-1)!="SOUTH" & word(block_rggvy,-1)!="NORTH"  
replace block_name_merge = block_name_merge_below if block_name_merge=="" & word(block_name_merge_below,-1)!="I" & word(block_name_merge_below,-1)!="II" & word(block_name_merge_below,-1)!="III" & word(block_name_merge_below,-1)!="WEST" & word(block_name_merge_below,-1)!="EAST" & word(block_name_merge_below,-1)!="SOUTH" & word(block_name_merge_below,-1)!="NORTH" & word(block_rggvy,-1)!="I" & word(block_rggvy,-1)!="II" & word(block_rggvy,-1)!="III" & word(block_rggvy,-1)!="WEST" & word(block_rggvy,-1)!="EAST" & word(block_rggvy,-1)!="SOUTH" & word(block_rggvy,-1)!="NORTH"  
gen replace_text = "replace block = " + q + block_name_merge + q + " if block==" + q + block_rggvy + q + " & district==" + q + district + q + " & state==" + q + state + q if block_name_merge!="" & word(block_name_merge,-1)!="I" & word(block_name_merge,-1)!="II" & word(block_name_merge,-1)!="III" & word(block_rggvy,-1)!="I" & word(block_rggvy,-1)!="II" & word(block_rggvy,-1)!="III"
br if replace_text!="" & block_name_merge!=block_rggvy
*/
drop if merge_bkrggvy==2
duplicates drop
rename block_rggvy block
replace bk_code_rggvy = . if block=="SIRPUR" & district=="ADILABAD" & state=="ANDHRA PRADESH"
merge m:m state st_code district dt_code block using "$hab9/hab_census09_for_merge.dta", nogen
order st_code state dt_code dt_code11 district block bk_code_pca bk_code11 bk_code_conc01 bk_code_conc11 bk_code_rggvy
sort  st_code state dt_code dt_code11 district block bk_code_pca bk_code11 bk_code_conc01 bk_code_conc11 bk_code_rggvy
save "$hab9/hab_census09_for_merge.dta", replace



use "$hab9/hab_census09_for_merge.dta", clear // try RGGVY hab list block names
keep st_code state dt_code dt_code11 district block merge*
rename block block_lh
rename district district11
duplicates drop
merge 1:m st_code state dt_code11 district11 block_lh using "$hmerge/pca_2001_names_merges_all.dta", gen(merge_bklh)
keep st_code state dt_code dt_code11 district11 block_lh bk_code_lh merge_bk merge_bk11 merge_bkconc01 merge_bkconc11 merge_bkrggvy merge_bklh
sort st_code dt_code11 block_lh
replace state = state[_n-1] if state=="" & st_code==st_code[_n-1]
replace state = state[_n+1] if state=="" & st_code==st_code[_n+1]
replace district = district[_n-1] if district=="" & dt_code11==dt_code11[_n-1]
replace district = district[_n+1] if district=="" & dt_code11==dt_code11[_n+1]
duplicates drop 
/*
keep if merge_bklh<3 & merge_bk!=3 & merge_bk11!=3 & merge_bkconc01!=3 & merge_bkconc11!=3 & merge_bkrggvy!=3
drop if block_lh==""
gen block_name_merge = ""  // create field for merge name, to go back and replace when insheeting habitation census
gen q = `"""'

*Iterate 2x to correct blocks that are 1-2 letters off
gen block_pca_above = block_lh[_n-1] if merge_bklh[_n-1]==2
gen block_pca_below = block_lh[_n+1] if merge_bklh[_n+1]==2
strdist block_lh block_pca_above if merge_bklh==1, gen(dist_above)
strdist block_lh block_pca_below if merge_bklh==1, gen(dist_below)
br block_lh block_pca_above dist_above if dist_above<3
br block_lh block_pca_below dist_below if dist_below<3
replace block_name_merge = block_pca_above if merge_bklh==1 & merge_bklh[_n-1]==2 & dist_above<3 & dist_below>3 & state==state[_n-1] & district==district[_n-1] 
replace block_name_merge = block_pca_below if merge_bklh==1 & merge_bklh[_n+1]==2 & dist_below<3 & dist_above>3 & state==state[_n+1] & district==district[_n+1] 
gen replace_text = "replace block = " + q + block_name_merge + q + " if block==" + q + block_lh + q + " & district==" + q + district + q + " & state==" + q + state + q if block_name_merge!="" & word(block_name_merge,-1)!="I" & word(block_name_merge,-1)!="II" & word(block_name_merge,-1)!="III" & word(block_lh,-1)!="I" & word(block_lh,-1)!="II" & word(block_lh,-1)!="III"
br if replace_text!="" & block_name_merge!=block_lh

*Iterate 1x to correct blocks that have a matching word
gen block_pca_above = block_lh[_n-1] if merge_bklh[_n-1]==2
gen block_pca_below = block_lh[_n+1] if merge_bklh[_n+1]==2
gen block_name_merge_above = block_pca_above if merge_bklh==1 & merge_bklh[_n-1]==2 & word(block_lh,1)==word(block_pca_above,1) & state==state[_n-1] & district==district[_n-1] 
gen block_name_merge_below = block_pca_below if merge_bklh==1 & merge_bklh[_n+1]==2 & word(block_lh,1)==word(block_pca_below,1) & state==state[_n+1] & district==district[_n+1] 
br state district block_lh block_pca* block_name_merge_* if block_name_merge_above!="" | block_name_merge_below!=""
replace block_name_merge = block_name_merge_above if block_name_merge=="" & word(block_name_merge_above,-1)!="I" & word(block_name_merge_above,-1)!="II" & word(block_name_merge_above,-1)!="III" & word(block_name_merge_above,-1)!="WEST" & word(block_name_merge_above,-1)!="EAST" & word(block_name_merge_above,-1)!="SOUTH" & word(block_name_merge_above,-1)!="NORTH" & word(block_lh,-1)!="I" & word(block_lh,-1)!="II" & word(block_lh,-1)!="III" & word(block_lh,-1)!="WEST" & word(block_lh,-1)!="EAST" & word(block_lh,-1)!="SOUTH" & word(block_lh,-1)!="NORTH"  
replace block_name_merge = block_name_merge_below if block_name_merge=="" & word(block_name_merge_below,-1)!="I" & word(block_name_merge_below,-1)!="II" & word(block_name_merge_below,-1)!="III" & word(block_name_merge_below,-1)!="WEST" & word(block_name_merge_below,-1)!="EAST" & word(block_name_merge_below,-1)!="SOUTH" & word(block_name_merge_below,-1)!="NORTH" & word(block_lh,-1)!="I" & word(block_lh,-1)!="II" & word(block_lh,-1)!="III" & word(block_lh,-1)!="WEST" & word(block_lh,-1)!="EAST" & word(block_lh,-1)!="SOUTH" & word(block_lh,-1)!="NORTH"  
gen replace_text = "replace block = " + q + block_name_merge + q + " if block==" + q + block_lh + q + " & district==" + q + district + q + " & state==" + q + state + q if block_name_merge!="" & word(block_name_merge,-1)!="I" & word(block_name_merge,-1)!="II" & word(block_name_merge,-1)!="III" & word(block_lh,-1)!="I" & word(block_lh,-1)!="II" & word(block_lh,-1)!="III"
br if replace_text!="" & block_name_merge!=block_lh
*/
drop if merge_bklh==2
duplicates drop
rename block_lh block
rename district11 district
replace bk_code_lh = . if block=="SIRPUR" & district=="ADILABAD" & state=="ANDHRA PRADESH"
merge m:m state st_code district dt_code11 block using "$hab9/hab_census09_for_merge.dta", nogen
*replace dt_code = . if regexm(district,"MEDINIPUR") & dt_code11!=.
order st_code state dt_code dt_code11 district block bk_code_pca bk_code11 bk_code_conc01 bk_code_conc11 bk_code_rggvy bk_code_lh
sort  st_code state dt_code dt_code11 district block bk_code_pca bk_code11 bk_code_conc01 bk_code_conc11 bk_code_rggvy bk_code_lh
save "$hab9/hab_census09_for_merge.dta", replace
}

********************************************************************************
********************************************************************************

** Step 10: Village matches, part III: Exact 2009 matches, where we know the block code
{
use "$hmerge/pca_2001_names_merges_all.dta", clear
foreach v of varlist bk_code* {
  assert `v'>=0
	replace `v'=-1 if `v'==.
}
rename village village_pca
rename block block_pca

*Find exact merges on village name, for all block code & village name combos
foreach bk of varlist bk_code_pca bk_code_conc01 bk_code_rggvy {
	local bk_stub = subinstr(substr("`bk'",8,99),"_","",.)
	gen h9v_id_`bk_stub' = .
	gen match9_`bk_stub'_vi = ""
	foreach vi of varlist village_pca village_vd village11 village_conc01 village_conc11 village_rggvy village_lh {
		local vi_stub = subinstr(substr("`vi'",8,99),"_","",.)
		rename `vi' village
	  merge m:m st_code dt_code `bk' village using "$hab9/hab_census09_for_merge.dta", keep(1 3) keepusing(h9v_id)
	  replace h9v_id_`bk_stub' = h9v_id if h9v_id!=. & h9v_id_`bk_stub'==. & _merge==3
		replace match9_`bk_stub'_vi = match9_`bk_stub'_vi + " `vi_stub'" if _merge==3 
		rename village `vi'
		drop h9v_id _merge
	}
}

foreach bk of varlist bk_code11 bk_code_conc11 bk_code_lh {
	local bk_stub = subinstr(substr("`bk'",8,99),"_","",.)
	gen h9v_id_`bk_stub' = .
	gen match9_`bk_stub'_vi = ""
	foreach vi of varlist village_pca village_vd village11 village_conc01 village_conc11 village_rggvy village_lh {
		local vi_stub = subinstr(substr("`vi'",8,99),"_","",.)
		rename `vi' village
	  merge m:m st_code dt_code11 `bk' village using "$hab9/hab_census09_for_merge.dta", keep(1 3) keepusing(h9v_id)
	  replace h9v_id_`bk_stub' = h9v_id if h9v_id!=. & h9v_id_`bk_stub'==. & _merge==3
		replace match9_`bk_stub'_vi = match9_`bk_stub'_vi + " `vi_stub'" if _merge==3 
		rename village `vi'
		drop h9v_id _merge
	}
}


*Create unified village match field and merge in from Hab Census data
duplicates drop
egen h9v_id_rowmin = rowmin(h9v_id_pca h9v_id_11 h9v_id_conc01 h9v_id_conc11 h9v_id_rggvy h9v_id_lh)
egen h9v_id_rowmax = rowmax(h9v_id_pca h9v_id_11 h9v_id_conc01 h9v_id_conc11 h9v_id_rggvy h9v_id_lh)
egen h9v_id_rowmin2 = rowmin(h9v_id_pca h9v_id_11 h9v_id_conc01 h9v_id_conc11 h9v_id_rggvy)
egen h9v_id_rowmax2 = rowmax(h9v_id_pca h9v_id_11 h9v_id_conc01 h9v_id_conc11 h9v_id_rggvy)
egen h9v_id_rowmin3 = rowmin(h9v_id_pca h9v_id_11 h9v_id_conc01 h9v_id_conc11)
egen h9v_id_rowmax3 = rowmax(h9v_id_pca h9v_id_11 h9v_id_conc01 h9v_id_conc11)
egen h9v_id_rowmean = rowmean(h9v_id_pca h9v_id_11 h9v_id_conc01 h9v_id_conc11 h9v_id_rggvy h9v_id_lh)
gen h9v_id = .
replace h9v_id = h9v_id_rowmin if h9v_id==. & h9v_id_rowmin==h9v_id_rowmax & h9v_id_rowmin!=. 
replace h9v_id = h9v_id_rowmin if h9v_id==. & h9v_id_rowmin2==h9v_id_rowmax2 & h9v_id_rowmin!=. 
replace h9v_id = h9v_id_rowmin if h9v_id==. & h9v_id_rowmin3==h9v_id_rowmax3 & h9v_id_rowmin!=.
replace h9v_id = h9v_id_rowmin if h9v_id==. & h9v_id_rowmin!=. & h9v_id_rowmean-h9v_id_rowmin<h9v_id_rowmax-h9v_id_rowmean
replace h9v_id = h9v_id_rowmax if h9v_id==. & h9v_id_rowmax!=. & h9v_id_rowmean-h9v_id_rowmin>h9v_id_rowmax-h9v_id_rowmean
foreach v of varlist h9v_id_* {
	replace h9v_id = `v' if `v'!=. & h9v_id==.
}
merge m:1 h9v_id using "$hab9/hab_census09_for_merge.dta", keep(1 3) keepusing(block village totp_h maxp_h count_h)
duplicates drop


*Remove duplicates, so a single PCA village doesn't match to multiple hab census villages
duplicates t names_id, gen(dup1)
sort names_id

  // drop the few where there are just straight duplicates of both master and using identifiers
duplicates drop names_id h9v_id, force

  // drop by population and whether certain dups have less consistent matches
gen pop_diff01 = abs(tot_p - totp_h)
gen pop_diff11 = abs(tot_p11 - totp_h)
drop if dup1>0 & names_id==names_id[_n-1] & ((pop_diff01!=. & pop_diff01>0 & pop_diff01[_n-1]==0) | (pop_diff11!=. & pop_diff11>0 & pop_diff11[_n-1]==0))
drop if dup1>0 & names_id==names_id[_n+1] & ((pop_diff01!=. & pop_diff01>0 & pop_diff01[_n+1]==0) | (pop_diff11!=. & pop_diff11>0 & pop_diff11[_n+1]==0))
drop if dup1>0 & names_id==names_id[_n-1] & (h9v_id>h9v_id_rowmin | h9v_id<h9v_id_rowmax) & (h9v_id[_n-1]==h9v_id_rowmin[_n-1] & h9v_id[_n-1]==h9v_id_rowmax[_n-1])
drop if dup1>0 & names_id==names_id[_n+1] & (h9v_id>h9v_id_rowmin | h9v_id<h9v_id_rowmax) & (h9v_id[_n+1]==h9v_id_rowmin[_n+1] & h9v_id[_n-1]==h9v_id_rowmax[_n+1])
drop if dup1>0 & names_id==names_id[_n-1] & h9v_id==h9v_id[_n-1] & h9v_id_rowmin[_n-1]==h9v_id_rowmax[_n-1] & h9v_id_rowmin<h9v_id_rowmax
drop if dup1>0 & names_id==names_id[_n+1] & h9v_id==h9v_id[_n+1] & h9v_id_rowmin[_n+1]==h9v_id_rowmax[_n+1] & h9v_id_rowmin<h9v_id_rowmax

  // drop if there's match discrepancies within a row
gen todrop1 = 0
replace todrop1 = 1 if dup1>0 & names_id==names_id[_n-1] & (h9v_id_rowmin2!=h9v_id_rowmin | h9v_id_rowmax2!=h9v_id_rowmax) 
replace todrop1 = 1 if dup1>0 & names_id==names_id[_n+1] & (h9v_id_rowmin2!=h9v_id_rowmin | h9v_id_rowmax2!=h9v_id_rowmax) 
egen todrop1_min = min(todrop1), by(names_id)
drop if todrop1==1 & todrop1_min==0

  // drop if the other duplicate has no match discrepancies
gen todrop2 = 0
replace todrop2 = 1 if dup1>0 & names_id==names_id[_n-1] & h9v_id[_n-1]==h9v_id_rowmin[_n-1] & h9v_id[_n-1]==h9v_id_rowmax[_n-1]
replace todrop2 = 1 if dup1>0 & names_id==names_id[_n+1] & h9v_id[_n+1]==h9v_id_rowmin[_n+1] & h9v_id[_n+1]==h9v_id_rowmax[_n+1]
egen todrop2_min = min(todrop2), by(names_id)
drop if todrop2==1 & todrop2_min==0

  // drop if only the other duplicate has a block that matches
gen todrop3 = 0
replace todrop3 = 1 if dup1>0 & names_id==names_id[_n-1] & block!=block[_n-1] & h9v_id[_n-1]==h9v_id_rowmin[_n-1] & h9v_id[_n-1]==h9v_id_rowmax[_n-1]
replace todrop3 = 1 if dup1>0 & names_id==names_id[_n+1] & h9v_id[_n+1]==h9v_id_rowmin[_n+1] & h9v_id[_n+1]==h9v_id_rowmax[_n+1]
egen todrop3_min = min(todrop3), by(names_id)
drop if todrop3==1 & todrop3_min==0

  // keep the ones where the block agrees most often
gen block_match9_count = block==block_pca
replace block_match9_count = block_match9_count + (block==block11)
replace block_match9_count = block_match9_count + (block==block_2001)
replace block_match9_count = block_match9_count + (block==block_2011)
replace block_match9_count = block_match9_count + (block==block_rggvy)
replace block_match9_count = block_match9_count + (block==block_lh)
drop if dup1>0 & names_id==names_id[_n-1] & block_match9_count<block_match9_count[_n-1]
drop if dup1>0 & names_id==names_id[_n+1] & block_match9_count<block_match9_count[_n+1]

  // for matches where block NEVER agrees -- reset hab variables to zero (this should take care of remaining dups)
replace h9v_id = . 		if _merge==3 & block_match9_count==0
replace block = "" 		if _merge==3 & block_match9_count==0
replace village = "" 	if _merge==3 & block_match9_count==0
replace totp_h = . 		if _merge==3 & block_match9_count==0
replace maxp_h = . 		if _merge==3 & block_match9_count==0
replace count_h = .		if _merge==3 & block_match9_count==0

duplicates t names_id if h9v_id!=., gen(dup1_check)
assert dup1_check==0 if h9v_id!=.



*Remove duplicates, so a single hab census village doesn't match to multiple PCA villages
duplicates t h9v_id, gen(dup2)
replace dup2 = . if h9v_id==.
sort h9v_id

	// pick the match with the closest 2001 population, within reason
egen min_pop_diff01 = min(pop_diff01), by(h9v_id)
egen min_pop_diff11 = min(pop_diff11), by(h9v_id)
gen dup2_keep = 0
replace dup2_keep = 1 if dup2>0 & dup2!=. & min_pop_diff01==pop_diff01 & (min_pop_diff01<=0.2*tot_p | min_pop_diff01<20)
egen dup2_keep_max1 = max(dup2_keep), by(h9v_id)

	// pick the match with the closest 2011 population, within reason
*br if dup2>0 & dup2<. & dup2_keep_max1==0
replace dup2_keep = 1 if dup2>0 & dup2!=. & dup2_keep_max1==0 & min_pop_diff11==pop_diff11 & (min_pop_diff11<=0.2*tot_p11 | min_pop_diff11<20)
egen dup2_keep_max2 = max(dup2_keep), by(h9v_id)

  // find dups where the total PCA population adds up to hab population (identifies BOTH
	// villages with identical names and non-unique PCA villages in the names master dataset)
*br if dup2>0 & dup2<. & dup2_keep_max2==0
egen sum_pop01 = sum(tot_p), by(h9v_id)
egen sum_pop11 = sum(tot_p11), by(h9v_id)
replace dup2_keep = 1 if dup2>0 & dup2!=. & dup2_keep_max2==0 & (abs(totp_h - sum_pop01)<20 | abs(totp_h - sum_pop01)<0.2*sum_pop01) 
egen dup2_keep_max3 = max(dup2_keep), by(h9v_id)
replace dup2_keep = 1 if dup2>0 & dup2!=. & dup2_keep_max3==0 & (abs(totp_h - sum_pop11)<20 | abs(totp_h - sum_pop11)<0.2*sum_pop11)
egen dup2_keep_max4 = max(dup2_keep), by(h9v_id)

  // trust the h9v_id that matched the most times
*br if dup2>0 & dup2<. & dup2_keep_max4==0
gen match9_count = h9v_id_pca!=.
replace match9_count = match9_count + (h9v_id_11!=.)
replace match9_count = match9_count + (h9v_id_conc01!=.)
replace match9_count = match9_count + (h9v_id_conc11!=.)
replace match9_count = match9_count + (h9v_id_rggvy!=.)
replace match9_count = match9_count + (h9v_id_lh!=.)
gen match9_all = trim(itrim(match9_pca_vi + " " + match9_11_vi + " " + match9_conc01_vi + " " + match9_conc11_vi + " " + match9_rggvy_vi + " " + match9_lh_vi))
egen match9_count2 = noccur(match9_all), string(" ")
egen match9_count_max = max(match9_count), by(h9v_id)
egen match9_count_min = min(match9_count), by(h9v_id)
egen match9_count2_max = max(match9_count2), by(h9v_id)
egen match9_count2_min = min(match9_count2), by(h9v_id)
replace dup2_keep = 1 if dup2>0 & dup2!=. & dup2_keep_max4==0 & match9_count2==match9_count2_max & match9_count2>match9_count2_min
replace dup2_keep = 1 if dup2>0 & dup2!=. & dup2_keep_max4==0 & match9_count==match9_count_max & match9_count>match9_count_min
egen dup2_keep_max5 = max(dup2_keep), by(h9v_id)

  // i'm content to live with these dups for now, they're less than 1% of matches
*br if dup2>0 & dup2<. & dup2_keep_max5==0
replace dup2_keep = 1 if dup2>0 & dup2!=. & dup2_keep_max5==0

  // reset h9v_id and hab census variables to blanks for the weaker of the duplicate matches
replace h9v_id = . 		if dup2>0 & dup2!=. & dup2_keep==0
replace block = "" 		if dup2>0 & dup2!=. & dup2_keep==0
replace village = "" 	if dup2>0 & dup2!=. & dup2_keep==0
replace totp_h = . 		if dup2>0 & dup2!=. & dup2_keep==0
replace maxp_h = . 		if dup2>0 & dup2!=. & dup2_keep==0
replace count_h = .		if dup2>0 & dup2!=. & dup2_keep==0
replace pop_diff01 = .	if dup2>0 & dup2!=. & dup2_keep==0
replace pop_diff11 = .	if dup2>0 & dup2!=. & dup2_keep==0
replace sum_pop01 = .		if dup2>0 & dup2!=. & dup2_keep==0
replace sum_pop11 = .		if dup2>0 & dup2!=. & dup2_keep==0

drop dup1* todrop1* todrop2* todrop3* _merge dup2* h9v_id_row* min_pop* match9_all match9_count2* match9_count_*

rename block block9
rename village village9
rename pop_diff01 pop_diff01_9
rename pop_diff11 pop_diff11_9
rename sum_pop01 sum_pop_01_9
rename sum_pop11 sum_pop_11_9

*Drop duplicates and bad matches, confirm a(n almost) 1-1 match
duplicates drop
duplicates r h9v_id
duplicates r names_id

*Split "names" dataset into matched and unmatched villages
preserve
keep if h9v_id!=.
save "$hmerge/pca_2001_names_merges_all_temp_matched9.dta", replace
restore
keep if h9v_id==.
drop h9v_id_pca-match9_count
duplicates drop
save "$hmerge/pca_2001_names_merges_all_temp_unmatched9.dta", replace

*Split hab census dataset into matched and unmatched villages
use "$hab9/hab_census09_for_merge.dta", clear
merge 1:m h9v_id using "$hmerge/pca_2001_names_merges_all_temp_matched9.dta", keepusing(h9v_id)
duplicates drop 
assert _merge!=2
preserve
keep if _merge==3
drop _merge
save "$hab9/hab_census09_for_merge_temp_matched.dta", replace
restore
keep if _merge==1
drop _merge
save "$hab9/hab_census09_for_merge_temp_unmatched.dta", replace
}

********************************************************************************
********************************************************************************

** Step 11: Village matches, part IV: Exact 2009 matches, where we don't know the block code (being conservative!)
{
*Grab villages without block matches from unmatched
use "$hab9/hab_census09_for_merge_temp_unmatched.dta", clear
keep if bk_code_pca==. & bk_code11==. & bk_code_conc01==. & bk_code_conc11==. & bk_code_rggvy==. & bk_code_lh==.
assert (dt_code!=. | dt_code11!=.)
save "$hab9/hab_census09_for_merge_temp_unmatched_nobk.dta", replace

*Merge on just st_code, dt_code, and village hame
use "$hmerge/pca_2001_names_merges_all_temp_unmatched9.dta", clear
gen block_nobk_dt01 = ""
gen h9v_id_nobk_dt01 = .
gen match9_nobk_vi_dt01 = ""
foreach vi of varlist village_pca village_vd village11 village_conc01 village_conc11 village_rggvy village_lh {
	local vi_stub = subinstr(substr("`vi'",8,99),"_","",.)
	rename `vi' village
	sort names_id
  merge m:m st_code dt_code village using "$hab9/hab_census09_for_merge_temp_unmatched_nobk.dta", keep(1 3) keepusing(h9v_id block)
  replace block_nobk_dt01 = block if block!="" & block_nobk_dt01=="" & _merge==3 
	replace h9v_id_nobk_dt01 = h9v_id if h9v_id!=. & h9v_id_nobk_dt01==. & _merge==3
	replace match9_nobk_vi_dt01 = match9_nobk_vi_dt01 + " `vi_stub'" if _merge==3 
	rename village `vi'
	drop block h9v_id _merge
}
duplicates drop

gen block_nobk_dt11 = ""
gen h9v_id_nobk_dt11 = .
gen match9_nobk_vi_dt11 = ""
foreach vi of varlist village_pca village_vd village11 village_conc01 village_conc11 village_rggvy village_lh {
	local vi_stub = subinstr(substr("`vi'",8,99),"_","",.)
	rename `vi' village
	sort names_id
  merge m:m st_code dt_code11 village using "$hab9/hab_census09_for_merge_temp_unmatched_nobk.dta", keep(1 3) keepusing(h9v_id block)
  replace block_nobk_dt11 = block if block!="" & block_nobk_dt11=="" & _merge==3 
	replace h9v_id_nobk_dt11 = h9v_id if h9v_id!=. & h9v_id_nobk_dt11==. & _merge==3
	replace match9_nobk_vi_dt11 = match9_nobk_vi_dt11 + " `vi_stub'" if _merge==3 
	rename village `vi'
	drop block h9v_id _merge
}
duplicates drop

gen h9v_id = .
replace h9v_id = h9v_id_nobk_dt01 if h9v_id==. & h9v_id_nobk_dt01!=. & h9v_id_nobk_dt11==.
replace h9v_id = h9v_id_nobk_dt11 if h9v_id==. & h9v_id_nobk_dt01==. & h9v_id_nobk_dt11!=.
replace h9v_id = h9v_id_nobk_dt01 if h9v_id==. & h9v_id_nobk_dt01!=. & h9v_id_nobk_dt11!=. & h9v_id_nobk_dt01==h9v_id_nobk_dt11

foreach v of varlist block_pca block_2001 block_rggvy {
  strdist block_nobk_dt01 `v' if `v'!="", gen(d_`v')
}
foreach v of varlist block11 block_2011 block_lh {
  strdist block_nobk_dt11 `v' if `v'!="", gen(d_`v')
}

egen d_01_min = rowmin(d_block_pca d_block_2001 d_block_rggvy)
egen d_11_min = rowmin(d_block11 d_block_2011 d_block_lh)
replace h9v_id = h9v_id_nobk_dt01 if h9v_id==. & h9v_id_nobk_dt01!=. & h9v_id_nobk_dt11!=. & d_01_min<d_11_min
replace h9v_id = h9v_id_nobk_dt11 if h9v_id==. & h9v_id_nobk_dt01!=. & h9v_id_nobk_dt11!=. & d_01_min>d_11_min
egen d_01_mean = rowmean(d_block_pca d_block_2001 d_block_rggvy)
egen d_11_mean = rowmean(d_block11 d_block_2011 d_block_lh)
replace h9v_id = h9v_id_nobk_dt01 if h9v_id==. & h9v_id_nobk_dt01!=. & h9v_id_nobk_dt11!=. & d_01_mean<d_11_mean
replace h9v_id = h9v_id_nobk_dt11 if h9v_id==. & h9v_id_nobk_dt01!=. & h9v_id_nobk_dt11!=. & d_01_mean>d_11_mean

merge m:1 h9v_id using "$hab9/hab_census09_for_merge_temp_unmatched_nobk.dta", keep(1 3) keepusing(block village totp_h9 maxp_h9 count_h9)
duplicates drop


*Remove duplicates, so a single PCA village doesn't match to multiple hab census villages
duplicates t names_id, gen(dup1)
sort names_id

  // drop the few where there are just straight duplicates of both master and using identifiers
duplicates drop names_id h9v_id, force

  // find where population exactly matches
gen pop_diff01 = abs(tot_p - totp_h9)
gen pop_diff11 = abs(tot_p11 - totp_h9)
drop if dup1>0 & names_id==names_id[_n-1] & ((pop_diff01!=. & pop_diff01>0 & pop_diff01[_n-1]==0) | (pop_diff11!=. & pop_diff11>0 & pop_diff11[_n-1]==0))
drop if dup1>0 & names_id==names_id[_n+1] & ((pop_diff01!=. & pop_diff01>0 & pop_diff01[_n+1]==0) | (pop_diff11!=. & pop_diff11>0 & pop_diff11[_n+1]==0))

egen pop_diff01_mean = mean(pop_diff01), by(block* district dt_code state st_code)
egen pop_diff11_mean = mean(pop_diff11), by(block* district dt_code state st_code)
gen pop_match = pop_diff01==0 | pop_diff11==0
egen pop_match_mean = mean(pop_match), by(block* district dt_code state st_code)
egen pop_match_count = sum(pop_match), by(block* district dt_code state st_code)
egen match_count = count(names_id) if h9v_id!=., by(block* district dt_code state st_code)

 // try to match a few more block names
/*
keep block* district dt_code state st_code h9v_id pop_diff* pop_match*
keep if h9v_id!=.
gen id = _n
collapse (count) id (sum) count_pop_match=pop_match (mean) pop_match pop_diff01 pop_diff11, by(block* district dt_code state st_code )
gsort -id

foreach v of varlist block_pca block11 block_2001 block_2011 block_rggvy block_lh {
  strdist block `v' if `v'!="", gen(d_`v')
}
egen d_min = rowmin(d_block*)

gen block_name_merge = ""
foreach v of varlist block_pca block11 block_2001 block_2011 block_rggvy block_lh {
  replace block_name_merge = `v' if block_name_merge=="" & d_`v'==d_min
}
gen q = `"""'
gen replace_text = "replace block = " + q + block_name_merge + q + " if block==" + q + block + q + " & district==" + q + district + q + " & state==" + q + state + q if block_name_merge!="" & id>=100
br if replace_text!="" & block_name_merge!=block & block_name_merge!=district
gen replace_text2= "replace block = " + q + block_name_merge + q + " if block==" + q + block + q + " & district==" + q + district + q + " & state==" + q + state + q if block_name_merge!="" & id>=50
br if replace_text=="" & replace_text2!="" & block_name_merge!=block & block_name_merge!=district
gen replace_text3= "replace block = " + q + block_name_merge + q + " if block==" + q + block + q + " & district==" + q + district + q + " & state==" + q + state + q if block_name_merge!="" & substr(block_name_merge,1,length(block))==block
br if replace_text=="" & replace_text2=="" & replace_text3!="" & block!=block_name_merge & block!="" & block_name_merge!=""
*/

*Create flag for names matches to keep
duplicates t names_id, gen(dup2)
gen keep_flag = 0
replace keep_flag = 1 if _merge==3 & (pop_match_mean>=0.5 & match_count>=20) & dup2==0
replace h9v_id = . 		if _merge==3 & keep_flag==0
replace block = "" 		if _merge==3 & keep_flag==0
replace village = "" 	if _merge==3 & keep_flag==0
replace totp_h9 = . 	if _merge==3 & keep_flag==0
replace maxp_h9 = . 	if _merge==3 & keep_flag==0
replace count_h9 = .	if _merge==3 & keep_flag==0
foreach v of varlist pop_diff01-match_count {
  replace `v' = .	if _merge==3 & keep_flag==0
}
duplicates drop
duplicates r names_id


*Remove duplicates, so a single hab census village doesn't match to multiple PCA villages
duplicates t h9v_id, gen(dup3)
replace dup3 = . if h9v_id==.
sort h9v_id

	// pick the match with the closest 2001 population, within reason
egen min_pop_diff01 = min(pop_diff01), by(h9v_id)
egen min_pop_diff11 = min(pop_diff11), by(h9v_id)
gen dup3_keep = 0
replace dup3_keep = 1 if dup3>0 & dup3!=. & min_pop_diff01==pop_diff01 & (min_pop_diff01<=0.2*tot_p | min_pop_diff01<20)
egen dup3_keep_max1 = max(dup3_keep), by(h9v_id)

	// pick the match with the closest 2011 population, within reason
*br if dup3>0 & dup3<. & dup3_keep_max1==0
replace dup3_keep = 1 if dup3>0 & dup3!=. & dup3_keep_max1==0 & min_pop_diff11==pop_diff11 & (min_pop_diff11<=0.2*tot_p11 | min_pop_diff11<20)
egen dup3_keep_max2 = max(dup3_keep), by(h9v_id)

  // find dups where the total PCA population adds up to hab population (identifies BOTH
	// villages with identical names and non-unique PCA villages in the names master dataset)
*br if dup3>0 & dup3<. & dup3_keep_max2==0
egen sum_pop01 = sum(tot_p), by(h9v_id)
egen sum_pop11 = sum(tot_p11), by(h9v_id)
replace dup3_keep = 1 if dup3>0 & dup3!=. & dup3_keep_max2==0 & (abs(totp_h9 - sum_pop01)<20 | abs(totp_h9 - sum_pop01)<0.2*sum_pop01) 
egen dup3_keep_max3 = max(dup3_keep), by(h9v_id)
replace dup3_keep = 1 if dup3>0 & dup3!=. & dup3_keep_max3==0 & (abs(totp_h9 - sum_pop11)<20 | abs(totp_h9 - sum_pop11)<0.2*sum_pop11)
egen dup3_keep_max4 = max(dup3_keep), by(h9v_id)

  // i'm content to live with these dups for now, they're less than 1% of matches
br if dup3>0 & dup3<. & dup3_keep_max4==0
replace dup3_keep = 1 if dup3>0 & dup3!=. & dup3_keep_max4==0

  // reset h9v_id and hab census variables to blanks for the weaker of the duplicate matches
replace h9v_id = . 		if dup3>0 & dup3!=. & dup3_keep==0
replace block = "" 		if dup3>0 & dup3!=. & dup3_keep==0
replace village = "" 	if dup3>0 & dup3!=. & dup3_keep==0
replace totp_h9 = . 		if dup3>0 & dup3!=. & dup3_keep==0
replace maxp_h9 = . 		if dup3>0 & dup3!=. & dup3_keep==0
replace count_h9 = .		if dup3>0 & dup3!=. & dup3_keep==0
replace pop_diff01 = .	if dup3>0 & dup3!=. & dup3_keep==0
replace pop_diff11 = .	if dup3>0 & dup3!=. & dup3_keep==0
replace sum_pop01 = .		if dup3>0 & dup3!=. & dup3_keep==0
replace sum_pop11 = .		if dup3>0 & dup3!=. & dup3_keep==0

drop dup1* dup2* dup3* keep* min_pop* pop_diff*_mean pop_match* match_count _merge d_*

rename block block9
rename village village9
rename pop_diff01 pop_diff01_9
rename pop_diff11 pop_diff11_9
rename sum_pop01 sum_pop_01_9
rename sum_pop11 sum_pop_11_9

*Drop duplicates and bad matches, confirm a(n almost) 1-1 match
duplicates drop
duplicates r h9v_id
duplicates r names_id

*Update "names" matched and unmatched datasets
preserve
keep if h9v_id!=.
append using "$hmerge/pca_2001_names_merges_all_temp_matched9.dta"
order st_code-vdpr3 h9v_id_pca-match9_lh_vi  
save "$hmerge/pca_2001_names_merges_all_temp_matched9.dta", replace
restore
keep if h9v_id==.
drop block_nobk_dt01-sum_pop_11_9
duplicates drop
save "$hmerge/pca_2001_names_merges_all_temp_unmatched9.dta", replace


*Split hab census dataset into matched and unmatched villages
use "$hab9/hab_census09_for_merge.dta", clear
merge 1:m h9v_id using "$hmerge/pca_2001_names_merges_all_temp_matched9.dta", keepusing(h9v_id)
duplicates drop 
assert _merge!=2
preserve
keep if _merge==3
drop _merge
save "$hab9/hab_census09_for_merge_temp_matched.dta", replace
restore
keep if _merge==1
drop _merge
save "$hab9/hab_census09_for_merge_temp_unmatched.dta", replace

}
  // send these unmatched files to the server for a fuzzy (reclink) merge
  // "merge_habitation_fuzzy_2009.do", Steps 1-4

********************************************************************************
********************************************************************************

** Step 12: Village matches, part V: Process 2003 reclink matches from server
  // get 2003 reclink merge results from server
{
use "$hmerge/pca_2001_names_matched_reclink_03.dta", clear
*br names_id h3v_id block3-pop_diff11_3 fscore3_pca fscore3_11 fscore3_rggvy fuzzy3_pca fuzzy3_11 fuzzy3_rggvy
egen fscore3 = rowmin(fscore3_pca fscore3_11 fscore3_rggvy)
gen fmatch3_bk = "pca" if fuzzy3_pca!=""
replace fmatch3_bk = fmatch3_bk + " 11" if fuzzy3_11!=""
replace fmatch3_bk = fmatch3_bk + " rggvy" if fuzzy3_rggvy!=""
gen fmatch3_vi = fuzzy3_pca if fuzzy3_pca!=""
replace fmatch3_vi = fmatch3_vi + " " + fuzzy3_11 if fuzzy3_11!="" & fuzzy3_11!=fmatch3_vi
replace fmatch3_vi = fmatch3_vi + " " + fuzzy3_rggvy if fuzzy3_rggvy!="" & fuzzy3_rggvy!=fmatch3_vi
replace fmatch3_bk = trim(itrim(fmatch3_bk))
replace fmatch3_vi = trim(itrim(fmatch3_vi))
drop fscore3_pca fscore3_11 fscore3_rggvy fuzzy3_pca fuzzy3_11 fuzzy3_rggvy

*Resolve the double and triple matches
sort names_id h3v_id fmatch3_bk fmatch3_vi
egen group_temp = group(names_id h3v_id)
gen fmatch3 = fmatch3_bk if group_temp>group_temp[_n-1] | _n==1
replace fmatch3 = fmatch3 + " " + fmatch3_bk[_n+1] if group_temp>group_temp[_n-1] & group_temp==group_temp[_n+1]
replace fmatch3 = fmatch3 + " " + fmatch3_bk[_n+2] if group_temp>group_temp[_n-1] & group_temp==group_temp[_n+1] & group_temp==group_temp[_n+2]
replace fmatch3 = subinstr(fmatch3,"11 pca","pca 11", .)
replace fmatch3 = fmatch3 + " (" + fmatch3_vi if fmatch3!=""
replace fmatch3 = fmatch3 + " " + fmatch3_vi[_n+1] if fmatch3!="" & group_temp==group_temp[_n+1] & fmatch3_vi[_n+1]!=fmatch3_vi
replace fmatch3 = fmatch3 + " " + fmatch3_vi[_n+2] if fmatch3!="" & group_temp==group_temp[_n+2] & fmatch3_vi!=fmatch3_vi[_n+2] & fmatch3_vi[_n+1]!=fmatch3_vi[_n+2]
replace fmatch3 = fmatch3 + ")" if fmatch3!=""
drop if fmatch3==""
assert group_temp==_n
drop fmatch3_bk fmatch3_vi group_temp


*Remove duplicates, so a single PCA village doesn't match to multiple hab census villages
duplicates t names_id, gen(dup1)
sort names_id
gen fvil = "_" + subinstr(subinstr(word(fmatch,-1),"(","",.),")","",.)
gen fvillage3 = ""
rename village11 village_11
foreach v of newlist _pca _vd _11 _conc01 _conc11 _rggvy _lh {
  replace fvillage3 = village`v' if fvil=="`v'"
}
rename village_11 village11

  // for the few where the hab matches are identical, keep both
duplicates t names_id totp_h3 maxp_h3 count_h3, gen(dup1a)

  // drop by population, where it exactly matches
gen pop_diff01 = abs(tot_p - totp_h3)
gen pop_diff11 = abs(tot_p11 - totp_h3)
drop if dup1>0 & names_id==names_id[_n-1] & ((pop_diff01!=. & pop_diff01>0 & pop_diff01[_n-1]==0) | (pop_diff11!=. & pop_diff11>0 & pop_diff11[_n-1]==0)) & dup1a==0
drop if dup1>0 & names_id==names_id[_n+1] & ((pop_diff01!=. & pop_diff01>0 & pop_diff01[_n+1]==0) | (pop_diff11!=. & pop_diff11>0 & pop_diff11[_n+1]==0)) & dup1a==0

  // drop based on string distance of village names and fscore (combined)
strdist fvillage3 village3, gen(d_vill3)
egen d_vill3_min = min(d_vill3), by(names_id)
egen fscore3_max = max(fscore3), by(names_id)
drop if dup1>0 & (names_id==names_id[_n-1] | names_id==names_id[_n+1]) & d_vill3>d_vill3_min & fscore3<fscore3_max & dup1a==0

  // drop by population, where it's close but not exactly matching
drop if dup1>0 & names_id==names_id[_n-1] & ((pop_diff01!=. & pop_diff01>20 & pop_diff01[_n-1]<20) | (pop_diff11!=. & pop_diff11>20 & pop_diff11[_n-1]<20)) & dup1a==0
drop if dup1>0 & names_id==names_id[_n+1] & ((pop_diff01!=. & pop_diff01>20 & pop_diff01[_n+1]<20) | (pop_diff11!=. & pop_diff11>20 & pop_diff11[_n+1]<20)) & dup1a==0
drop if dup1>0 & names_id==names_id[_n-1] & ((pop_diff01!=. & pop_diff01>.2*tot_p & pop_diff01[_n-1]<.2*tot_p) | (pop_diff11!=. & pop_diff11>.2*tot_p11 & pop_diff11[_n-1]<.2*tot_p11)) & dup1a==0
drop if dup1>0 & names_id==names_id[_n+1] & ((pop_diff01!=. & pop_diff01>.2*tot_p & pop_diff01[_n+1]<.2*tot_p) | (pop_diff11!=. & pop_diff11>.2*tot_p11 & pop_diff11[_n+1]<.2*tot_p11)) & dup1a==0

  // keep 30 remaining dupes for now (to take h3v_id's out of circulation)


*Remove duplicates, so a single hab census village doesn't match to multiple PCA villages
duplicates t h3v_id, gen(dup2)
sort h3v_id

	// pick the match with the closest 2001 population, within reason
egen min_pop_diff01 = min(pop_diff01), by(h3v_id)
egen min_pop_diff11 = min(pop_diff11), by(h3v_id)
gen dup2_keep = 0
replace dup2_keep = 1 if dup2>0 & dup2!=. & min_pop_diff01==pop_diff01 & (min_pop_diff01<=0.2*tot_p | min_pop_diff01<20)
egen dup2_keep_max1 = max(dup2_keep), by(h3v_id)

	// pick the match with the closest 2011 population, within reason
*br if dup2>0 & dup2<. & dup2_keep_max1==0
replace dup2_keep = 1 if dup2>0 & dup2!=. & dup2_keep_max1==0 & min_pop_diff11==pop_diff11 & (min_pop_diff11<=0.2*tot_p11 | min_pop_diff11<20)
egen dup2_keep_max2 = max(dup2_keep), by(h3v_id)

  // find dups where the total PCA population adds up to hab population (identifies BOTH
	// villages with identical names and non-unique PCA villages in the names master dataset)
*br if dup2>0 & dup2<. & dup2_keep_max2==0
egen sum_pop01 = sum(tot_p), by(h3v_id)
egen sum_pop11 = sum(tot_p11), by(h3v_id)
replace dup2_keep = 1 if dup2>0 & dup2!=. & dup2_keep_max2==0 & (abs(totp_h3 - sum_pop01)<20 | abs(totp_h3 - sum_pop01)<0.2*sum_pop01) 
egen dup2_keep_max3 = max(dup2_keep), by(h3v_id)
replace dup2_keep = 1 if dup2>0 & dup2!=. & dup2_keep_max3==0 & (abs(totp_h3 - sum_pop11)<20 | abs(totp_h3 - sum_pop11)<0.2*sum_pop11)
egen dup2_keep_max4 = max(dup2_keep), by(h3v_id)

  // drop based on string distance and fscore (combined
egen d_vill3_min2 = min(d_vill3), by(h3v_id)
egen fscore3_max2 = max(fscore3), by(h3v_id)
replace dup2_keep = 1 if dup2>0 & dup2!=. & dup2_keep_max4==0 & d_vill3==d_vill3_min2 & fscore3==fscore3_max2
egen dup2_keep_max5 = max(dup2_keep), by(h3v_id)

  // i'm content to live with these dups for now, and take them out of circulation
*br if dup2>0 & dup2<. & dup2_keep_max5==0
replace dup2_keep = 1 if dup2>0 & dup2!=. & dup2_keep_max5==0

  // drop weaker of the h3v_id duplicate matches and prep for merging
drop if dup2>0 & dup2!=. & dup2_keep==0
keep names_id h3v_id block3 village3 totp_h3 maxp_h3 count_h3 pop_diff01_3 pop_diff11_3 sum_pop_01_3 sum_pop_11_3 fscore3 fmatch3 fvill fvillage3 d_vill3


*Append to matched3 dataset and remove from unmatched3 dataset
preserve
merge m:1 names_id using "$hmerge/pca_2001_names_merges_all_temp_unmatched3.dta", keep(3) nogen
append using "$hmerge/pca_2001_names_merges_all_temp_matched3.dta"
order names_id st_code-match3_count fmatch3 fscore3 fvillage3 d_vill3
compress
save "$hmerge/pca_2001_names_merges_all_temp_matched3.dta", replace
restore
keep names_id 
duplicates drop  
merge 1:1 names_id using "$hmerge/pca_2001_names_merges_all_temp_unmatched3.dta", keep(2) nogen
save "$hmerge/pca_2001_names_merges_all_temp_unmatched3.dta", replace

*Split hab census dataset into matched and unmatched villages
use "$hab3/hab_census03_for_merge.dta", clear
merge 1:m h3v_id using "$hmerge/pca_2001_names_merges_all_temp_matched3.dta", keepusing(h3v_id)
duplicates drop 
assert _merge!=2
preserve
keep if _merge==3
drop _merge
save "$hab3/hab_census03_for_merge_temp_matched.dta", replace
restore
keep if _merge==1
drop _merge
save "$hab3/hab_census03_for_merge_temp_unmatched.dta", replace

}	
  // send remaining unmatched files to the server for a fuzzy (masala) merge
  // "merge_habitation_fuzzy_2003.do", Step 5

********************************************************************************
********************************************************************************

** Step 13: Village matches, part VI: Process 2009 reclink matches from server
  // get 2009 reclink merge results from server
{
use "$hmerge/pca_2001_names_matched_reclink_09.dta", clear
*br names_id h9v_id block9-pop_diff11_9 fscore9_pca fscore9_11 fscore9_rggvy fuzzy9_pca fuzzy9_11 fuzzy9_rggvy
egen fscore9 = rowmin(fscore9_pca fscore9_11 fscore9_rggvy)
gen fmatch9_bk = "pca" if fuzzy9_pca!=""
replace fmatch9_bk = fmatch9_bk + " 11" if fuzzy9_11!=""
replace fmatch9_bk = fmatch9_bk + " rggvy" if fuzzy9_rggvy!=""
gen fmatch9_vi = fuzzy9_pca if fuzzy9_pca!=""
replace fmatch9_vi = fmatch9_vi + " " + fuzzy9_11 if fuzzy9_11!="" & fuzzy9_11!=fmatch9_vi
replace fmatch9_vi = fmatch9_vi + " " + fuzzy9_rggvy if fuzzy9_rggvy!="" & fuzzy9_rggvy!=fmatch9_vi
replace fmatch9_bk = trim(itrim(fmatch9_bk))
replace fmatch9_vi = trim(itrim(fmatch9_vi))
drop fscore9_pca fscore9_11 fscore9_rggvy fuzzy9_pca fuzzy9_11 fuzzy9_rggvy

*Resolve the double and triple matches
sort names_id h9v_id fmatch9_bk fmatch9_vi
egen group_temp = group(names_id h9v_id)
gen fmatch9 = fmatch9_bk if group_temp>group_temp[_n-1] | _n==1
replace fmatch9 = fmatch9 + " " + fmatch9_bk[_n+1] if group_temp>group_temp[_n-1] & group_temp==group_temp[_n+1]
replace fmatch9 = fmatch9 + " " + fmatch9_bk[_n+2] if group_temp>group_temp[_n-1] & group_temp==group_temp[_n+1] & group_temp==group_temp[_n+2]
replace fmatch9 = subinstr(fmatch9,"11 pca","pca 11", .)
replace fmatch9 = fmatch9 + " (" + fmatch9_vi if fmatch9!=""
replace fmatch9 = fmatch9 + " " + fmatch9_vi[_n+1] if fmatch9!="" & group_temp==group_temp[_n+1] & fmatch9_vi[_n+1]!=fmatch9_vi
replace fmatch9 = fmatch9 + " " + fmatch9_vi[_n+2] if fmatch9!="" & group_temp==group_temp[_n+2] & fmatch9_vi!=fmatch9_vi[_n+2] & fmatch9_vi[_n+1]!=fmatch9_vi[_n+2]
replace fmatch9 = fmatch9 + ")" if fmatch9!=""
drop if fmatch9==""
assert group_temp==_n
drop fmatch9_bk fmatch9_vi group_temp


*Remove duplicates, so a single PCA village doesn't match to multiple hab census villages
duplicates t names_id, gen(dup1)
sort names_id
gen fvil = "_" + subinstr(subinstr(word(fmatch,-1),"(","",.),")","",.)
gen fvillage9 = ""
rename village11 village_11
foreach v of newlist _pca _vd _11 _conc01 _conc11 _rggvy _lh {
  replace fvillage9 = village`v' if fvil=="`v'"
}
rename village_11 village11

  // for the few where the hab matches are identical, keep both
duplicates t names_id totp_h9 maxp_h9 count_h9, gen(dup1a)

  // drop by population, where it exactly matches
gen pop_diff01 = abs(tot_p - totp_h9)
gen pop_diff11 = abs(tot_p11 - totp_h9)
drop if dup1>0 & names_id==names_id[_n-1] & ((pop_diff01!=. & pop_diff01>0 & pop_diff01[_n-1]==0) | (pop_diff11!=. & pop_diff11>0 & pop_diff11[_n-1]==0)) & dup1a==0
drop if dup1>0 & names_id==names_id[_n+1] & ((pop_diff01!=. & pop_diff01>0 & pop_diff01[_n+1]==0) | (pop_diff11!=. & pop_diff11>0 & pop_diff11[_n+1]==0)) & dup1a==0

  // drop based on string distance of village names and fscore (combined)
strdist fvillage9 village9, gen(d_vill9)
egen d_vill9_min = min(d_vill9), by(names_id)
egen fscore9_max = max(fscore9), by(names_id)
drop if dup1>0 & (names_id==names_id[_n-1] | names_id==names_id[_n+1]) & d_vill9>d_vill9_min & fscore9<fscore9_max & dup1a==0

  // drop by population, where it's close but not exactly matching
drop if dup1>0 & names_id==names_id[_n-1] & ((pop_diff01!=. & pop_diff01>20 & pop_diff01[_n-1]<20) | (pop_diff11!=. & pop_diff11>20 & pop_diff11[_n-1]<20)) & dup1a==0
drop if dup1>0 & names_id==names_id[_n+1] & ((pop_diff01!=. & pop_diff01>20 & pop_diff01[_n+1]<20) | (pop_diff11!=. & pop_diff11>20 & pop_diff11[_n+1]<20)) & dup1a==0
drop if dup1>0 & names_id==names_id[_n-1] & ((pop_diff01!=. & pop_diff01>.2*tot_p & pop_diff01[_n-1]<.2*tot_p) | (pop_diff11!=. & pop_diff11>.2*tot_p11 & pop_diff11[_n-1]<.2*tot_p11)) & dup1a==0
drop if dup1>0 & names_id==names_id[_n+1] & ((pop_diff01!=. & pop_diff01>.2*tot_p & pop_diff01[_n+1]<.2*tot_p) | (pop_diff11!=. & pop_diff11>.2*tot_p11 & pop_diff11[_n+1]<.2*tot_p11)) & dup1a==0

  // keep 80 remaining dupes for now (to take h9v_id's out of circulation)


*Remove duplicates, so a single hab census village doesn't match to multiple PCA villages
duplicates t h9v_id, gen(dup2)
sort h9v_id

	// pick the match with the closest 2001 population, within reason
egen min_pop_diff01 = min(pop_diff01), by(h9v_id)
egen min_pop_diff11 = min(pop_diff11), by(h9v_id)
gen dup2_keep = 0
replace dup2_keep = 1 if dup2>0 & dup2!=. & min_pop_diff01==pop_diff01 & (min_pop_diff01<=0.2*tot_p | min_pop_diff01<20)
egen dup2_keep_max1 = max(dup2_keep), by(h9v_id)

	// pick the match with the closest 2011 population, within reason
*br if dup2>0 & dup2<. & dup2_keep_max1==0
replace dup2_keep = 1 if dup2>0 & dup2!=. & dup2_keep_max1==0 & min_pop_diff11==pop_diff11 & (min_pop_diff11<=0.2*tot_p11 | min_pop_diff11<20)
egen dup2_keep_max2 = max(dup2_keep), by(h9v_id)

  // find dups where the total PCA population adds up to hab population (identifies BOTH
	// villages with identical names and non-unique PCA villages in the names master dataset)
*br if dup2>0 & dup2<. & dup2_keep_max2==0
egen sum_pop01 = sum(tot_p), by(h9v_id)
egen sum_pop11 = sum(tot_p11), by(h9v_id)
replace dup2_keep = 1 if dup2>0 & dup2!=. & dup2_keep_max2==0 & (abs(totp_h9 - sum_pop01)<20 | abs(totp_h9 - sum_pop01)<0.2*sum_pop01) 
egen dup2_keep_max9 = max(dup2_keep), by(h9v_id)
replace dup2_keep = 1 if dup2>0 & dup2!=. & dup2_keep_max9==0 & (abs(totp_h9 - sum_pop11)<20 | abs(totp_h9 - sum_pop11)<0.2*sum_pop11)
egen dup2_keep_max4 = max(dup2_keep), by(h9v_id)

  // drop based on string distance and fscore (combined
egen d_vill9_min2 = min(d_vill9), by(h9v_id)
egen fscore9_max2 = max(fscore9), by(h9v_id)
replace dup2_keep = 1 if dup2>0 & dup2!=. & dup2_keep_max4==0 & d_vill9==d_vill9_min2 & fscore9==fscore9_max2
egen dup2_keep_max5 = max(dup2_keep), by(h9v_id)

  // i'm content to live with these dups for now, and take them out of circulation
*br if dup2>0 & dup2<. & dup2_keep_max5==0
replace dup2_keep = 1 if dup2>0 & dup2!=. & dup2_keep_max5==0

  // drop weaker of the h9v_id duplicate matches and prep for merging
drop if dup2>0 & dup2!=. & dup2_keep==0
keep names_id h9v_id block9 village9 totp_h9 maxp_h9 count_h9 pop_diff01_9 pop_diff11_9 sum_pop_01_9 sum_pop_11_9 fscore9 fmatch9 fvill fvillage9 d_vill9


*Append to matched9 dataset and remove from unmatched9 dataset
preserve
merge m:1 names_id using "$hmerge/pca_2001_names_merges_all_temp_unmatched9.dta", keep(3) nogen
append using "$hmerge/pca_2001_names_merges_all_temp_matched9.dta"
order names_id st_code-match9_count fmatch9 fscore9 fvillage9 d_vill9
compress
save "$hmerge/pca_2001_names_merges_all_temp_matched9.dta", replace
restore
keep names_id 
duplicates drop  
merge 1:1 names_id using "$hmerge/pca_2001_names_merges_all_temp_unmatched9.dta", keep(2) nogen
save "$hmerge/pca_2001_names_merges_all_temp_unmatched9.dta", replace

*Split hab census dataset into matched and unmatched villages
use "$hab9/hab_census09_for_merge.dta", clear
merge 1:m h9v_id using "$hmerge/pca_2001_names_merges_all_temp_matched9.dta", keepusing(h9v_id)
duplicates drop 
assert _merge!=2
preserve
keep if _merge==3
drop _merge
save "$hab9/hab_census09_for_merge_temp_matched.dta", replace
restore
keep if _merge==1
drop _merge
save "$hab9/hab_census09_for_merge_temp_unmatched.dta", replace

}	
  // send remaining unmatched files to the server for a fuzzy (masala) merge
  // "merge_habitation_fuzzy_2009.do", Step 5
		
********************************************************************************
********************************************************************************

** Step 14: Village matches, part VII: Process 2003 masala matches from server
  // get 2003 masala merge results from server
{
*Drop duplicates from MMout03_all, "reshape", and calculate M-L distance
use "$hmerge/MMout03_all.dta", clear
sort  names_id h3v_id bk vi
*br names_id h3v_id bk vi

  // drop MM  non-block matches, if that same names_id has a block match
gen no_bk = bk=="nobk" | bk=="nobk11"
egen no_bk_min = min(no_bk), by(names_id h3v_id)  
drop if no_bk==1 & no_bk_min==0

  // "reshape" bk and vi by hand
egen group1 = group(names_id h3v_id)
sort group1 bk vi
gen bk_MM_matches = ""
replace bk_MM_matches = bk if group1>group1[_n-1] | _n==1
forvalues i =1/4 {
  replace bk_MM_matches = bk_MM_matches + " " + bk[_n+`i'] if bk_MM_matches!="" & group1[_n+`i']==group1[_n] 
}
gen vi_MM_matches = ""
replace vi_MM_matches = vi if group1>group1[_n-1] | _n==1
forvalues i =1/20 {
  replace vi_MM_matches = vi_MM_matches + " " + vi[_n+`i'] if vi_MM_matches!="" & group1[_n+`i']==group1[_n] & vi[_n+`i']!=vi[_n+`i'-1]
}
replace vi_MM_matches = trim(subinstr(subinstr(vi_MM_matches,"village","",.),"_","",.))
egen temp1 = mode(bk_MM_matches), by(group1)
egen temp2 = mode(vi_MM_matches), by(group1)
replace bk_MM_matches = temp1 if bk_MM_matches=="" 
replace vi_MM_matches = temp2 if vi_MM_matches=="" 
replace bk_MM_matches = subinstr(bk_MM_matches,"11 11","11",.)
replace bk_MM_matches = subinstr(bk_MM_matches,"11 11","11",.)
replace bk_MM_matches = subinstr(bk_MM_matches,"pca pca","pca",.)
replace bk_MM_matches = subinstr(bk_MM_matches,"pca pca","pca",.)
replace bk_MM_matches = subinstr(bk_MM_matches,"rggvy rggvy","rggvy",.)
replace bk_MM_matches = subinstr(bk_MM_matches,"rggvy rggvy","rggvy",.)
replace bk_MM_matches = subinstr(bk_MM_matches,"nobk nobk","nobk",.)
replace bk_MM_matches = subinstr(bk_MM_matches,"nobk nobk","nobk",.)
replace vi_MM_matches = subinstr(vi_MM_matches,"conc01 lh vd 11 conc01 lh vd 11","conc01 lh vd 11",.)
replace vi_MM_matches = subinstr(vi_MM_matches,"11 conc01 pca 11 conc01 pca","11 conc01 pca",.)
replace vi_MM_matches = subinstr(vi_MM_matches,"11 lh vd 11 lh vd","11 lh vd",.)
replace vi_MM_matches = subinstr(vi_MM_matches,"11 pca 11 pca","11 pca",.)
replace vi_MM_matches = subinstr(vi_MM_matches,"11 pca 11 pca","11 pca",.)
replace vi_MM_matches = subinstr(vi_MM_matches,"11 rggvy 11 rggvy","11 rggvy",.)
replace vi_MM_matches = subinstr(vi_MM_matches,"11 vd 11 vd","11 vd",.)
replace vi_MM_matches = subinstr(vi_MM_matches,"11 vd 11 vd","11 vd",.)
replace vi_MM_matches = subinstr(vi_MM_matches,"lh pca lh pca","lh pca",.)
replace vi_MM_matches = subinstr(vi_MM_matches,"11 conc01 lh vd 11 conc01 vd","11 conc01 lh vd",.)
replace vi_MM_matches = subinstr(vi_MM_matches,"11 lh vd 11 vd","11 lh vd",.)
compress

  // step down multiple (identical) village matches, for the same (names_id, h3v_id, bk)
gen vi_pca = vi=="village_pca"
egen vi_pca_max = max(vi_pca), by(names_id h3v_id bk)
drop if vi_pca_max==1 & vi_pca==0
gen vi_vd = vi=="village_vd"
egen vi_vd_max = max(vi_vd), by(names_id h3v_id bk)
drop if vi_pca_max==0 & vi_vd_max==1 & vi_vd==0
gen vi_11 = vi=="village11"
egen vi_11_max = max(vi_11), by(names_id h3v_id bk)
drop if vi_pca_max==0 & vi_vd_max==0 & vi_11_max==1 & vi_11==0
gen vi_conc01 = vi=="village_conc01"
egen vi_conc01_max = max(vi_conc01), by(names_id h3v_id bk)
drop if vi_pca_max==0 & vi_vd_max==0 & vi_11_max==0 & vi_conc01_max==1 & vi_conc01==0
gen vi_conc11 = vi=="village_conc11"
egen vi_conc11_max = max(vi_conc11), by(names_id h3v_id bk)
drop if vi_pca_max==0 & vi_vd_max==0 & vi_11_max==0 & vi_conc01_max==0 & vi_conc11_max==1 & vi_conc11==0
gen vi_rggvy = vi=="village_rggvy"
egen vi_rggvy_max = max(vi_rggvy), by(names_id h3v_id bk)
drop if vi_pca_max==0 & vi_vd_max==0 & vi_11_max==0 & vi_conc01_max==0 & vi_conc11_max==0 & vi_rggvy_max==1 & vi_rggvy==0
gen vi_lh = vi=="village_lh"
egen vi_lh_max = max(vi_lh), by(names_id h3v_id bk)
drop if vi_pca_max==0 & vi_vd_max==0 & vi_11_max==0 & vi_conc01_max==0 & vi_conc11_max==0 & vi_rggvy_max==0 & vi_lh_max==1 & vi_lh==0

  // drop duplicates after having reshaped and kept preferred village match
drop bk vi vi_pca-vi_lh_max temp1 temp2
duplicates drop
duplicates r names_id h3v_id
duplicates r names_id
duplicates r h3v_id

  // find masala-levenshtein distance for each remaining MM_match
*do "$hab3/merge/masala_merge_lp.do"
gen Mvillage = village_pca if regexm(vi_MM_matches,"pca")
replace Mvillage = village_vd if regexm(vi_MM_matches,"vd") & Mvillage==""
replace Mvillage = village11 if regexm(vi_MM_matches,"11") & Mvillage==""
replace Mvillage = village_conc01 if regexm(vi_MM_matches,"conc01") & Mvillage==""
replace Mvillage = village_conc11 if regexm(vi_MM_matches,"conc11") & Mvillage==""
replace Mvillage = village_rggvy if regexm(vi_MM_matches,"rggvy") & Mvillage==""
replace Mvillage = village_lh if regexm(vi_MM_matches,"lh") & Mvillage==""
*masala_lev_dist village3 Mvillage, gen(mldist)


*Verify MM_matches with blocks, based on string distance and population, and resolve dupes
 *br names_id h3v_id bk_MM_matches vi_MM_matches Mvillage village3 lev_dist if bk_MM!="nobk"

  // drop the few names_id dupes
sort names_id h3v_id
duplicates t names_id if bk_MM!="nobk", gen(dup1)
replace dup1 = 0 if dup1==.
*br names_id h3v_id bk_MM_matches vi_MM_matches Mvillage village3 lev_dist if bk_MM!="nobk" & dup1>0
drop if dup1>0  // only 9 villages affected

  // resolve h3v_id dupes
duplicates t h3v_id if bk_MM!="nobk", gen(dup2)
replace dup2 = 0 if dup2==.
gen pop_diff01 = abs(tot_p - totp_h3)
gen pop_diff11 = abs(tot_p11 - totp_h3)
sort no_bk h3v_id
drop if dup2>0 & pop_diff01>=0 & ((h3v_id==h3v_id[_n-1] & pop_diff01[_n-1]==0) | (h3v_id==h3v_id[_n+1] & pop_diff01[_n+1]==0))
drop if dup2>0 & pop_diff11>=0 & ((h3v_id==h3v_id[_n-1] & pop_diff11[_n-1]==0) | (h3v_id==h3v_id[_n+1] & pop_diff11[_n+1]==0))
drop if dup2>0 & ((h3v_id==h3v_id[_n-1] & pop_diff01[_n-1]<pop_diff01) | (h3v_id==h3v_id[_n+1] & pop_diff01[_n+1]<pop_diff01))
drop if dup2>0 & ((h3v_id==h3v_id[_n-1] & pop_diff11[_n-1]<pop_diff11) | (h3v_id==h3v_id[_n+1] & pop_diff11[_n+1]<pop_diff11))

duplicates t h3v_id if bk_MM!="nobk", gen(dup2b)
replace dup2b = 0 if dup2b==.
*br names_id h3v_id bk_MM_matches vi_MM_matches Mvillage village3 lev_dist pop_diff* if bk_MM!="nobk" & dup2b>0
drop if dup2b>0 // only a handful of villages affected

  // keep the acceptable matches
gen keep_flag = 0 if bk_MM!="nobk"	
replace keep_flag = 1 if keep_flag==0 & (pop_diff01<=20 | pop_diff01<=0.2*tot_p) & pop_diff01!=.
replace keep_flag = 1 if keep_flag==0 & (pop_diff11<=20 | pop_diff11<=0.2*tot_p11) & pop_diff11!=.
*br names_id h3v_id bk_MM_matches vi_MM_matches Mvillage village3 lev_dist pop_diff* if bk_MM!="nobk" & keep_flag==0
sort lev_dist
replace keep_flag = 1 if keep_flag==0
			// after looking closely through remaming MM_matches, the script appears to have done an EXCELLENT job! 
			// i see very few bad matches, so i'm accepting all of them.

  // drop any nobk matches that are now irrelevant, since we have a bk match
egen max_keep_flag1 = max(keep_flag), by(names_id)
egen max_keep_flag2 = max(keep_flag), by(h3v_id)
drop if keep_flag==. & max_keep_flag1==1 & pop_diff01>0 & pop_diff11>0  // drop names_id's with pending nobk matches, because they're no longer on the market
drop if keep_flag==. & max_keep_flag2==1 & pop_diff01>0 & pop_diff11>0  // drop h3v_id's with pending nobk matches, because they're no longer on the market

  // resolve the few obs where the nobk match might be better than the bk match
replace keep_flag = 3 if keep_flag==. & max_keep_flag1==1 
replace keep_flag = 4 if keep_flag==. & max_keep_flag2==1
egen max_keep_flag3 = max(keep_flag), by(names_id)
egen max_keep_flag4 = max(keep_flag), by(h3v_id)
replace keep_flag = 0 if keep_flag==1 & max_keep_flag3==3
replace keep_flag = 1 if keep_flag==3 & max_keep_flag3==3
replace keep_flag = 0 if keep_flag==1 & max_keep_flag4==4
replace keep_flag = 1 if keep_flag==4 & max_keep_flag4==4
*br names_id h3v_id bk_MM_matches vi_MM_matches Mvillage village3 lev_dist pop_diff* keep_flag if max_keep_flag4==4

*Verify MM_matches with blocks, based on string distance and population, and resolve dupes
replace keep_flag = 2 if keep_flag==. & (pop_diff01==0 | pop_diff11==0)
duplicates t names_id if keep_flag==2, gen(dup3)
replace dup3 = 0 if dup3==.
assert dup3==0 // no names_id duplicates
duplicates t h3v_id if keep_flag==2, gen(dup4)
replace dup4 = 0 if dup4==.
*br names_id h3v_id bk_MM_matches vi_MM_matches Mvillage village3 lev_dist pop_diff* keep_flag if dup4>0
duplicates t pca01_id, gen(dup_pca)
drop if dup4>0 & dup_pca==0 // all remaining h3v_id dupes appear to be PCA dupes, which will get processed below
drop if keep_flag==0 | keep_flag==.

*Prep to append to matched dataset
keep names_id h3v_id count_h3 totp_h3 maxp_h3 block3 village3 bk_MM vi_MM pop_diff* lev_dist
duplicates r names_id
duplicates r h3v_id
rename pop_diff01 pop_diff01_3
rename pop_diff11 pop_diff11_3
duplicates drop names_id, force

*Append to matched3 dataset and remove from unmatched3 dataset
preserve
merge 1:1 names_id using "$hmerge/pca_2001_names_merges_all_temp_unmatched3.dta", keep(3) nogen
append using "$hmerge/pca_2001_names_merges_all_temp_matched3.dta"
order names_id st_code-vdpr3 h3v_id_pca-d_vill3 bk_MM vi_MM lev_dist h3v_id block3 village3 totp_h3 maxp_h3 count_h3 pop_diff01_3 pop_diff11_3
compress
save "$hmerge/pca_2001_names_merges_all_temp_matched3.dta", replace
restore
keep names_id 
duplicates drop  
merge 1:1 names_id using "$hmerge/pca_2001_names_merges_all_temp_unmatched3.dta", keep(2) nogen
save "$hmerge/pca_2001_names_merges_all_temp_unmatched3.dta", replace

*Split hab census dataset into matched and unmatched villages
use "$hab3/hab_census03_for_merge.dta", clear
merge 1:m h3v_id using "$hmerge/pca_2001_names_merges_all_temp_matched3.dta", keepusing(h3v_id)
duplicates drop 
assert _merge!=2
preserve
keep if _merge==3
drop _merge
save "$hab3/hab_census03_for_merge_temp_matched.dta", replace
restore
keep if _merge==1
drop _merge
save "$hab3/hab_census03_for_merge_temp_unmatched.dta", replace
}	

********************************************************************************
********************************************************************************

** Step 15: Village matches, part VIII: Process 2009 masala matches from server
  // get 2009 masala merge results from server
{
*Drop duplicates from MMout09_all, "reshape", and calculate M-L distance
use "$hmerge/MMout09_all.dta", clear
sort  names_id h9v_id bk vi
*br names_id h9v_id bk vi

  // drop MM  non-block matches, if that same names_id has a block match
gen no_bk = bk=="nobk" | bk=="nobk11"
egen no_bk_min = min(no_bk), by(names_id h9v_id)  
drop if no_bk==1 & no_bk_min==0

  // "reshape" bk and vi by hand
egen group1 = group(names_id h9v_id)
sort group1 bk vi
gen bk_MM_matches = ""
replace bk_MM_matches = bk if group1>group1[_n-1] | _n==1
forvalues i =1/4 {
  replace bk_MM_matches = bk_MM_matches + " " + bk[_n+`i'] if bk_MM_matches!="" & group1[_n+`i']==group1[_n] 
}
gen vi_MM_matches = ""
replace vi_MM_matches = vi if group1>group1[_n-1] | _n==1
forvalues i =1/20 {
  replace vi_MM_matches = vi_MM_matches + " " + vi[_n+`i'] if vi_MM_matches!="" & group1[_n+`i']==group1[_n] & vi[_n+`i']!=vi[_n+`i'-1]
}
replace vi_MM_matches = trim(subinstr(subinstr(vi_MM_matches,"village","",.),"_","",.))
egen temp1 = mode(bk_MM_matches), by(group1)
egen temp2 = mode(vi_MM_matches), by(group1)
replace bk_MM_matches = temp1 if bk_MM_matches=="" 
replace vi_MM_matches = temp2 if vi_MM_matches=="" 
replace bk_MM_matches = subinstr(bk_MM_matches,"11 11","11",.)
replace bk_MM_matches = subinstr(bk_MM_matches,"11 11","11",.)
replace bk_MM_matches = subinstr(bk_MM_matches,"pca pca","pca",.)
replace bk_MM_matches = subinstr(bk_MM_matches,"pca pca","pca",.)
replace bk_MM_matches = subinstr(bk_MM_matches,"rggvy rggvy","rggvy",.)
replace bk_MM_matches = subinstr(bk_MM_matches,"rggvy rggvy","rggvy",.)
replace bk_MM_matches = subinstr(bk_MM_matches,"nobk11 nobk11","nobk11",.)
replace bk_MM_matches = subinstr(bk_MM_matches,"nobk11 nobk11","nobk11",.)
replace bk_MM_matches = subinstr(bk_MM_matches,"nobk nobk ","nobk ",.)
replace bk_MM_matches = subinstr(bk_MM_matches,"nobk nobk ","nobk ",.)
replace bk_MM_matches = "nobk" if bk_MM_matches=="nobk nobk"
replace vi_MM_matches = subinstr(vi_MM_matches,"conc01 lh vd 11 conc01 lh vd 11","conc01 lh vd 11",.)
replace vi_MM_matches = subinstr(vi_MM_matches,"11 conc01 pca 11 conc01 pca","11 conc01 pca",.)
replace vi_MM_matches = subinstr(vi_MM_matches,"11 lh vd 11 lh vd","11 lh vd",.)
replace vi_MM_matches = subinstr(vi_MM_matches,"11 pca 11 pca","11 pca",.)
replace vi_MM_matches = subinstr(vi_MM_matches,"11 pca 11 pca","11 pca",.)
replace vi_MM_matches = subinstr(vi_MM_matches,"11 rggvy 11 rggvy","11 rggvy",.)
replace vi_MM_matches = subinstr(vi_MM_matches,"11 vd 11 vd","11 vd",.)
replace vi_MM_matches = subinstr(vi_MM_matches,"11 vd 11 vd","11 vd",.)
replace vi_MM_matches = subinstr(vi_MM_matches,"lh pca lh pca","lh pca",.)
replace vi_MM_matches = subinstr(vi_MM_matches,"conc01 conc11 conc01 conc11","conc01 conc11",.)
replace vi_MM_matches = subinstr(vi_MM_matches,"conc01 pca conc01 pca","conc01 pca",.)
replace vi_MM_matches = subinstr(vi_MM_matches,"11 conc01 lh vd 11 conc01 vd","11 conc01 lh vd",.)
replace vi_MM_matches = subinstr(vi_MM_matches,"11 lh vd 11 vd","11 lh vd",.)
replace vi_MM_matches = subinstr(vi_MM_matches,"lh pca lh","lh pca",.)
replace vi_MM_matches = subinstr(vi_MM_matches,"lh pca","lh pca",.)
replace vi_MM_matches = subinstr(vi_MM_matches,"11 rggvy 11","11 rggvy",.)
replace vi_MM_matches = subinstr(vi_MM_matches,"conc01 vd conc01","conc01 vd",.)
replace vi_MM_matches = subinstr(vi_MM_matches,"lh vd lh vd","lh vd",.)
replace vi_MM_matches = subinstr(vi_MM_matches,"pca vd pca vd","pca vd",.)
replace vi_MM_matches = subinstr(vi_MM_matches,"pca vd pca vd","pca vd",.)
replace vi_MM_matches = subinstr(vi_MM_matches,"pca rggvy pca rggvy","pca rggvy",.)
replace vi_MM_matches = subinstr(vi_MM_matches,"vd pca vd","pca vd",.)
replace vi_MM_matches = subinstr(vi_MM_matches,"11 conc01 11 conc01","11 conc01",.)
replace vi_MM_matches = subinstr(vi_MM_matches,"11 lh 11 lh","11 lh",.)
replace vi_MM_matches = subinstr(vi_MM_matches,"11 pca lh 11 pca lh","11 pca lh",.)
replace vi_MM_matches = subinstr(vi_MM_matches,"11 pca vd 11 pca vd","11 pca vd",.)
replace vi_MM_matches = subinstr(vi_MM_matches,"pca pca","pca",.)
replace vi_MM_matches = subinstr(vi_MM_matches,"lh pca vd lh pca vd","lh pca vd",.)
replace vi_MM_matches = subinstr(vi_MM_matches,"pca 11 pca","pca 11",.)
replace vi_MM_matches = subinstr(vi_MM_matches,"conc01 vd vd","conc01 vd",.)
replace vi_MM_matches = subinstr(vi_MM_matches,"conc11 pca conc11 pca","conc11 pca",.)
replace vi_MM_matches = subinstr(vi_MM_matches,"conc11 lh conc11 lh","conc11 lh",.)
replace vi_MM_matches = subinstr(vi_MM_matches,"pca lh pca","lh pca",.)
replace vi_MM_matches = subinstr(vi_MM_matches,"pca lh","lh pca",.)
replace vi_MM_matches = subinstr(vi_MM_matches,"pca 11","11 pca",.)
replace vi_MM_matches = subinstr(vi_MM_matches,"11 lh 11 pca lh pca","11 lh pca",.)
replace vi_MM_matches = subinstr(vi_MM_matches,"11 lh vd 11 lh vd","11 lh vd",.)
compress

  // step down multiple (identical) village matches, for the same (names_id, h9v_id, bk)
gen vi_pca = vi=="village_pca"
egen vi_pca_max = max(vi_pca), by(names_id h9v_id bk)
drop if vi_pca_max==1 & vi_pca==0
gen vi_vd = vi=="village_vd"
egen vi_vd_max = max(vi_vd), by(names_id h9v_id bk)
drop if vi_pca_max==0 & vi_vd_max==1 & vi_vd==0
gen vi_11 = vi=="village11"
egen vi_11_max = max(vi_11), by(names_id h9v_id bk)
drop if vi_pca_max==0 & vi_vd_max==0 & vi_11_max==1 & vi_11==0
gen vi_conc01 = vi=="village_conc01"
egen vi_conc01_max = max(vi_conc01), by(names_id h9v_id bk)
drop if vi_pca_max==0 & vi_vd_max==0 & vi_11_max==0 & vi_conc01_max==1 & vi_conc01==0
gen vi_conc11 = vi=="village_conc11"
egen vi_conc11_max = max(vi_conc11), by(names_id h9v_id bk)
drop if vi_pca_max==0 & vi_vd_max==0 & vi_11_max==0 & vi_conc01_max==0 & vi_conc11_max==1 & vi_conc11==0
gen vi_rggvy = vi=="village_rggvy"
egen vi_rggvy_max = max(vi_rggvy), by(names_id h9v_id bk)
drop if vi_pca_max==0 & vi_vd_max==0 & vi_11_max==0 & vi_conc01_max==0 & vi_conc11_max==0 & vi_rggvy_max==1 & vi_rggvy==0
gen vi_lh = vi=="village_lh"
egen vi_lh_max = max(vi_lh), by(names_id h9v_id bk)
drop if vi_pca_max==0 & vi_vd_max==0 & vi_11_max==0 & vi_conc01_max==0 & vi_conc11_max==0 & vi_rggvy_max==0 & vi_lh_max==1 & vi_lh==0

  // drop duplicates after having reshaped and kept preferred village match
drop bk vi vi_pca-vi_lh_max temp1 temp2
duplicates drop
duplicates t names_id h9v_id, gen(test)
egen temp1 = min(lev_dist), by(group1)
drop if test>0 & temp1<lev_dist
drop temp1 test
duplicates r names_id h9v_id
duplicates r names_id
duplicates r h9v_id

  // find masala-levenshtein distance for each remaining MM_match
*do "$hab9/merge/masala_merge_lp.do"
gen Mvillage = village_pca if regexm(vi_MM_matches,"pca")
replace Mvillage = village_vd if regexm(vi_MM_matches,"vd") & Mvillage==""
replace Mvillage = village11 if regexm(vi_MM_matches,"11") & Mvillage==""
replace Mvillage = village_conc01 if regexm(vi_MM_matches,"conc01") & Mvillage==""
replace Mvillage = village_conc11 if regexm(vi_MM_matches,"conc11") & Mvillage==""
replace Mvillage = village_rggvy if regexm(vi_MM_matches,"rggvy") & Mvillage==""
replace Mvillage = village_lh if regexm(vi_MM_matches,"lh") & Mvillage==""
*masala_lev_dist village9 Mvillage, gen(mldist)


*Verify MM_matches with blocks, based on string distance and population, and resolve dupes
 *br names_id h9v_id bk_MM_matches vi_MM_matches Mvillage village9 lev_dist if bk_MM!="nobk"

  // drop the few names_id dupes
sort names_id h9v_id
duplicates t names_id if regexm(bk_MM,"nobk")==0, gen(dup1)
replace dup1 = 0 if dup1==.
*br names_id h9v_id bk_MM_matches vi_MM_matches Mvillage village9 lev_dist if regexm(bk_MM,"nobk")==0 & dup1>0
drop if dup1>0  // only 14 villages affected

  // resolve h9v_id dupes
duplicates t h9v_id if regexm(bk_MM,"nobk")==0, gen(dup2)
replace dup2 = 0 if dup2==.
gen pop_diff01 = abs(tot_p - totp_h9)
gen pop_diff11 = abs(tot_p11 - totp_h9)
sort no_bk h9v_id
drop if dup2>0 & pop_diff01>=0 & ((h9v_id==h9v_id[_n-1] & pop_diff01[_n-1]==0) | (h9v_id==h9v_id[_n+1] & pop_diff01[_n+1]==0))
drop if dup2>0 & pop_diff11>=0 & ((h9v_id==h9v_id[_n-1] & pop_diff11[_n-1]==0) | (h9v_id==h9v_id[_n+1] & pop_diff11[_n+1]==0))
drop if dup2>0 & ((h9v_id==h9v_id[_n-1] & pop_diff01[_n-1]<pop_diff01) | (h9v_id==h9v_id[_n+1] & pop_diff01[_n+1]<pop_diff01))
drop if dup2>0 & ((h9v_id==h9v_id[_n-1] & pop_diff11[_n-1]<pop_diff11) | (h9v_id==h9v_id[_n+1] & pop_diff11[_n+1]<pop_diff11))

duplicates t h9v_id if regexm(bk_MM,"nobk")==0, gen(dup2b)
replace dup2b = 0 if dup2b==.
*br names_id h9v_id bk_MM_matches vi_MM_matches Mvillage village9 lev_dist pop_diff* if regexm(bk_MM,"nobk")==0 & dup2b>0
drop if dup2b>0 // only a handful of villages affected

  // keep the acceptable matches
gen keep_flag = 0 if regexm(bk_MM,"nobk")==0	
replace keep_flag = 1 if keep_flag==0 & (pop_diff01<=20 | pop_diff01<=0.2*tot_p) & pop_diff01!=.
replace keep_flag = 1 if keep_flag==0 & (pop_diff11<=20 | pop_diff11<=0.2*tot_p11) & pop_diff11!=.
*br names_id h9v_id bk_MM_matches vi_MM_matches Mvillage village9 lev_dist pop_diff* if regexm(bk_MM,"nobk")==0 & keep_flag==0
sort lev_dist
replace keep_flag = 1 if keep_flag==0
			// after looking closely through remaming MM_matches, the script appears to have done an EXCELLENT job! 
			// i see very few bad matches, so i'm accepting all of them.

  // drop any nobk matches that are now irrelevant, since we have a bk match
egen max_keep_flag1 = max(keep_flag), by(names_id)
egen max_keep_flag2 = max(keep_flag), by(h9v_id)
drop if keep_flag==. & max_keep_flag1==1 & pop_diff01>0 & pop_diff11>0  // drop names_id's with pending nobk matches, because they're no longer on the market
drop if keep_flag==. & max_keep_flag2==1 & pop_diff01>0 & pop_diff11>0  // drop h9v_id's with pending nobk matches, because they're no longer on the market

  // resolve the few obs where the nobk match might be better than the bk match
replace keep_flag = 3 if keep_flag==. & max_keep_flag1==1 
replace keep_flag = 4 if keep_flag==. & max_keep_flag2==1
egen max_keep_flag3 = max(keep_flag), by(names_id)
egen max_keep_flag4 = max(keep_flag), by(h9v_id)
replace keep_flag = 0 if keep_flag==1 & max_keep_flag3==3
replace keep_flag = 1 if keep_flag==3 & max_keep_flag3==3
replace keep_flag = 0 if keep_flag==1 & max_keep_flag4==4
replace keep_flag = 1 if keep_flag==4 & max_keep_flag4==4
*br names_id h9v_id bk_MM_matches vi_MM_matches Mvillage village9 lev_dist pop_diff* keep_flag if max_keep_flag4==4

*Verify MM_matches with blocks, based on string distance and population, and resolve dupes
replace keep_flag = 2 if keep_flag==. & (pop_diff01==0 | pop_diff11==0)
duplicates t names_id if keep_flag==2, gen(dup3)
replace dup3 = 0 if dup3==.
assert dup3==0 // no names_id duplicates
*br names_id h9v_id bk_MM_matches vi_MM_matches Mvillage village9 block9 block* lev_dist pop_diff* keep_flag if keep_flag==.
duplicates t h9v_id if keep_flag==2, gen(dup4)
replace dup4 = 0 if dup4==.
*br names_id h9v_id bk_MM_matches vi_MM_matches Mvillage village9 lev_dist pop_diff* keep_flag if dup4>0
duplicates t pca01_id, gen(dup_pca)
drop if dup4>0 & dup_pca==0 // all remaining h9v_id dupes appear to be PCA dupes, which will get processed below
replace keep_flag = 9 if keep_flag==. & lev_dist<=0.2
drop if keep_flag==0 | keep_flag==.
gen no_block_match = keep_flag==9 // keeping these around in case we want them, although I'm thinking we won't
duplicates drop names_id, force // these are all the no_block_matches, so I don't care too much

*Prep to append to matched dataset
keep names_id h9v_id count_h9 totp_h9 maxp_h9 block9 village9 bk_MM vi_MM pop_diff* lev_dist no_block_match
duplicates r names_id
duplicates r h9v_id
rename pop_diff01 pop_diff01_9
rename pop_diff11 pop_diff11_9
duplicates drop names_id, force

*Append to matched9 dataset and remove from unmatched9 dataset
preserve
merge 1:1 names_id using "$hmerge/pca_2001_names_merges_all_temp_unmatched9.dta", keep(3) nogen
append using "$hmerge/pca_2001_names_merges_all_temp_matched9.dta"
order names_id st_code-vdpr3 h9v_id_pca-d_vill9 bk_MM vi_MM lev_dist no_block_match h9v_id block9 village9 totp_h9 maxp_h9 count_h9 pop_diff01_9 pop_diff11_9
compress
save "$hmerge/pca_2001_names_merges_all_temp_matched9.dta", replace
restore
keep names_id 
duplicates drop  
merge 1:1 names_id using "$hmerge/pca_2001_names_merges_all_temp_unmatched9.dta", keep(2) nogen
save "$hmerge/pca_2001_names_merges_all_temp_unmatched9.dta", replace

*Split hab census dataset into matched and unmatched villages
use "$hab9/hab_census09_for_merge.dta", clear
merge 1:m h9v_id using "$hmerge/pca_2001_names_merges_all_temp_matched9.dta", keepusing(h9v_id)
duplicates drop 
assert _merge!=2
preserve
keep if _merge==3
drop _merge
save "$hab9/hab_census09_for_merge_temp_matched.dta", replace
restore
keep if _merge==1
drop _merge
save "$hab9/hab_census09_for_merge_temp_unmatched.dta", replace
}	
	
********************************************************************************
********************************************************************************

** Step 16: Village matches, part IX: Process all 2003 matches, remove dups
{	
use "$hmerge/pca_2001_names_merges_all_temp_matched3.dta", clear
duplicates t names_id, gen(dup1)

*Resolve names_id dupes, once and for all
sort names_id
egen sum_totp_hab=sum(totp_h3), by(names_id) 
gen pop_diff01_sum = abs(tot_p - sum_totp_hab)
gen pop_diff11_sum = abs(tot_p11 - sum_totp_hab)
*br names_id pca01_id h3v_id tot_p tot_p11 totp_h3 pop_diff01_sum pop_diff11_sum if dup1>0
count if dup1>0 & (pop_diff01_sum<=20 | pop_diff11_sum<=20 | pop_diff01_sum<=0.2*tot_p | pop_diff11_sum<=0.2*tot_p11)
count if dup1>0
drop if dup1>0 // 0.02% of names_ids, so easiest just to drop them, and they were all fuzzy merges


*Resolve h3v_id dupes, once and for all
duplicates t h3v_id, gen(dup2)	
sort h3v_id
*br if dup2>0

  // flag duplicates that are already in names_id
duplicates t h3v_id pca01_id, gen(dup_pca)
*br names_id pca01_id vd_id pca11_id h3v_id dup* if dup2>0 & dup2==dup_pca
gen names_collapse = ""
replace names_collapse = "pca dup" if dup2>0 & dup2==dup_pca & pca01_id!=.

  // flag duplicates where count_h3>1 and the total population is clsoe to adding up
*br names_id pca01_id vd_id pca11_id h3v_id tot_p tot_p11 totp_h sum_pop* count* names_collapse if dup2>0 & names_collapse==""
replace names_collapse = "hab split 01" if dup2>0 & names_collapse=="" & count_h3>1 & tot_p!=. & (abs(sum_pop_01_3-totp_h3)<20 | abs(sum_pop_01_3-totp_h3)<0.2*tot_p)
replace names_collapse = "hab split 11" if dup2>0 & names_collapse=="" & count_h3>1 & tot_p11!=. &(abs(sum_pop_11_3-totp_h3)<20 | abs(sum_pop_11_3-totp_h3)<0.2*tot_p11)

  // flag remaining duplicates with count_h3>1 as ones to split and check
replace names_collapse = "hab split ??" if dup2>0 & names_collapse=="" & count_h3>1

  // drop unsplittable h3v_id dups
drop if dup2>0 & names_collapse=="" & count_h3==1
gen row_id = _n

  // merge in uncollapsed hab03 dataset to try to split the dupes
joinby h3v_id using "$hab3/hab_census03_for_merge_all.dta", unmatched(master)
drop _merge block village 
replace totp = . if substr(names_collapse,1,9)!="hab split"
replace h3_id = . if substr(names_collapse,1,9)!="hab split"
*br names_id h3v_id tot_p tot_p11 totp_h sum_pop* count* names_collapse totp if dup2>0 & substr(names_collapse,1,9)=="hab split"

  // for 2-habitation villages, split evenly based on closest population
gen temp_diff01 = abs(totp - tot_p)
gen temp_diff11 = abs(totp - tot_p11)
egen temp_diff01_min1 = min(temp_diff01), by(names_id)
egen temp_diff11_min1 = min(temp_diff11), by(names_id)
egen temp_diff01_min2 = min(temp_diff01), by(h3_id)
egen temp_diff11_min2 = min(temp_diff11), by(h3_id)
gen hab_split_keep = ""
replace hab_split_keep = "keep 01"  if dup2==1 & temp_diff01==temp_diff01_min1 & temp_diff01==temp_diff01_min2 & names_collapse=="hab split 01" & count_h3==2 & tot_p!=. & hab_split_keep== ""
replace hab_split_keep = "keep 11"  if dup2==1 & temp_diff11==temp_diff11_min1 & temp_diff11==temp_diff11_min2 & names_collapse=="hab split 11" & count_h3==2 & tot_p11!=. & hab_split_keep==""
replace hab_split_keep = "keep 01?" if dup2==1 & temp_diff01==temp_diff01_min1 & temp_diff01==temp_diff01_min2 & names_collapse=="hab split ??" & count_h3==2 & tot_p!=. & hab_split_keep==""
replace hab_split_keep = "keep 11?" if dup2==1 & temp_diff11==temp_diff11_min1 & temp_diff11==temp_diff11_min2 & names_collapse=="hab split ??" & count_h3==2 & tot_p11!=. & hab_split_keep==""
gen keep_flag = (hab_split_keep!="")*h3_id
egen max_keep_flag1 = max(keep_flag), by(names_id)
egen min_keep_flag1 = min(keep_flag), by(names_id)
egen max_keep_flag2 = max(keep_flag), by(h3_id)
egen min_keep_flag2 = min(keep_flag), by(h3_id)
egen max_keep_flag3 = max(max_keep_flag1), by(h3v_id)
egen min_keep_flag3 = min(max_keep_flag1), by(h3v_id)
drop if dup2==1 & count_h3==2 & hab_split_keep=="" & max_keep_flag3>min_keep_flag3 & min_keep_flag3>0
replace hab_split_keep = "" if word(hab_split_keep,1)=="keep" & (max_keep_flag3==min_keep_flag3 | min_keep_flag3==0)
replace totp_h3 = totp if word(hab_split_keep,1)=="keep" 
replace maxp_h3 = totp if word(hab_split_keep,1)=="keep" 
replace count_h3 = 1 if word(hab_split_keep,1)=="keep"
gen h3_id_dupes = h3_id if word(hab_split_keep,1)=="keep"
drop sum_totp_hab pop_diff01_sum pop_diff11_sum dup_pca habitation scp stp totp h3_id temp_* *keep_flag*
duplicates drop
sort row_id
duplicates t row_id, gen(dup2b)
drop if dup2b==1 & substr(hab_split_keep,-1,1)=="?" & ((row_id==row_id[_n-1] & substr(hab_split_keep[_n-1],-1,1)!="?") | (row_id==row_id[_n+1] & substr(hab_split_keep[_n+1],-1,1)!="?"))
drop if dup2b==1 & ((row_id==row_id[_n-1] & abs(totp_h3-tot_p)>abs(totp_h3[_n-1]-tot_p[_n-1])) | (row_id==row_id[_n+1] & abs(totp_h3-tot_p)>abs(totp_h3[_n+1]-tot_p[_n+1])))
assert row_id==_n


  // for 2+habitation villages with 2+ dupes, find habitations that match village pop closely
duplicates t h3v_id if hab_split_keep=="" & names_collapse!="pca dup", gen(dup2c)	
replace dup2c = 0 if dup2c==.

joinby h3v_id using "$hab3/hab_census03_for_merge_all.dta", unmatched(master)
drop _merge block village 
replace totp = . if dup2c==0 
replace h3_id = . if dup2c==0

gen temp_diff01 = abs(totp - tot_p)
gen temp_diff11 = abs(totp - tot_p11)
egen temp_diff01_min1 = min(temp_diff01), by(names_id)
egen temp_diff11_min1 = min(temp_diff11), by(names_id)
egen temp_diff01_min2 = min(temp_diff01), by(h3_id)
egen temp_diff11_min2 = min(temp_diff11), by(h3_id)
/*replace hab_split_keep = "keep 01"  if dup2c>0 & temp_diff01==temp_diff01_min1 & temp_diff01==temp_diff01_min2 & names_collapse=="hab split 01" & count_h3>1 & tot_p!=. & hab_split_keep== ""
replace hab_split_keep = "keep 11"  if dup2c>0 & temp_diff11==temp_diff11_min1 & temp_diff11==temp_diff11_min2 & names_collapse=="hab split 11" & count_h3>1 & tot_p11!=. & hab_split_keep==""
replace hab_split_keep = "keep 01?" if dup2c>0 & temp_diff01==temp_diff01_min1 & temp_diff01==temp_diff01_min2 & names_collapse=="hab split ??" & count_h3>1 & tot_p!=. & hab_split_keep==""
replace hab_split_keep = "keep 11?" if dup2c>0 & temp_diff11==temp_diff11_min1 & temp_diff11==temp_diff11_min2 & names_collapse=="hab split ??" & count_h3>1 & tot_p11!=. & hab_split_keep==""
replace hab_split_keep = "keep ??"  if dup2c>0 & temp_diff11==temp_diff11_min1 & temp_diff11==temp_diff11_min2 & count_h3>1 & tot_p11!=. & hab_split_keep==""
gen keep_flag = (hab_split_keep!="")*h3_id
egen max_keep_flag1 = max(keep_flag), by(names_id)
egen min_keep_flag1 = min(keep_flag), by(names_id)
egen max_keep_flag2 = max(keep_flag), by(h3_id)
egen min_keep_flag2 = min(keep_flag), by(h3_id)
egen max_keep_flag3 = max(max_keep_flag1), by(h3v_id)
egen min_keep_flag3 = min(max_keep_flag1), by(h3v_id)
br names_id	h3v_id	tot_p	tot_p11	totp_h3	count_h3	names_collapse	hab_split_keep	dup2c	totp temp_diff01	temp_diff11	temp_diff01_min1	temp_diff11_min1	temp_diff01_min2	temp_diff11_min2	keep_flag	max_keep_flag1	min_keep_flag1	max_keep_flag2	min_keep_flag2	max_keep_flag3	min_keep_flag3 if dup2c>0
*/  
  // this is super tricky, because of the permutations of habitations and pca villages, so i'll only deal with those habitations that have 
	// exact population matches with pca village, since it's the singletons that we care about anyways.
*br names_id h3v_id tot_p tot_p11 totp_h3 count_h3 names_collapse hab_split_keep dup2c totp temp_diff01 temp_diff11 temp_diff01_min1 temp_diff11_min1 temp_diff01_min2 temp_diff11_min2 if dup2c>0 & (temp_diff01==0 | temp_diff11==0)
gen keep_flag = (dup2c>0 & (temp_diff01==0 | temp_diff11==0))*h3_id
egen max_keep_flag1 = max(keep_flag), by(names_id)
egen min_keep_flag1 = min(keep_flag), by(names_id)
egen max_keep_flag2 = max(keep_flag), by(h3_id)
egen min_keep_flag2 = min(keep_flag), by(h3_id)
egen max_keep_flag3 = max(max_keep_flag1), by(h3v_id)
egen min_keep_flag3 = min(max_keep_flag1), by(h3v_id)
sort keep_flag h3v_id
gen to_drop = 0
replace to_drop = 1 if keep_flag==keep_flag[_n-1] & keep_flag>0 & keep_flag!=.
replace to_drop = 1 if keep_flag==keep_flag[_n+1] & keep_flag>0 & keep_flag!=.
replace keep_flag = 0 if to_drop==1   // this gets rid of habitations matched to multiple villages
sort names_id keep_flag 
replace to_drop = 1 if names_id==names_id[_n-1] & names_id!=names_id[_n+1] & keep_flag>0 & keep_flag!=. & keep_flag[_n-1]>0 & keep_flag[_n-1]!=. & keep_flag[_n-1]<keep_flag
replace to_drop = 1 if names_id==names_id[_n-1] & names_id==names_id[_n+1] & names_id!=names_id[_n+2] & keep_flag>0 & keep_flag!=. & keep_flag[_n-1]>0 & keep_flag[_n-1]!=. & keep_flag[_n-1]<keep_flag
replace keep_flag = 0 if to_drop==1  // the next line gets rid of mutiple exact habitation matches within a village, which is almost certainly a duplicate (with slight variant) in the original habitation census
egen max_keep_flag4 = max(keep_flag), by(names_id)
assert keep_flag==0 | keep_flag==. | keep_flag==max_keep_flag4
replace totp_h3 = totp if keep_flag>0 & keep_flag!=.  
replace maxp_h3 = totp if keep_flag>0 & keep_flag!=. 
replace count_h3 = 1 if keep_flag>0 & keep_flag!=. 
replace h3_id_dupes = h3_id if keep_flag>0 & keep_flag!=. & h3_id_dupes==.
drop if max_keep_flag4>0 & max_keep_flag4!=. & keep_flag==0
drop habitation scp stp totp h3_id temp_* *keep_flag* to_drop
duplicates drop
sort row_id
assert row_id==_n

  // drop remaining h3v_id dupes
duplicates t h3v_id if names_collapse!="pca dup" & h3_id_dupes==., gen(dup2d)
replace dup2d = 0 if dup2d==.
drop if dup2d>0
drop dup*
compress
save "$hmerge/pca_2001_names_merges_all_matched3_processed.dta", replace
}

********************************************************************************
********************************************************************************

** Step 17: Village matches, part X: Process all 2009 matches, remove dups
{	
use "$hmerge/pca_2001_names_merges_all_temp_matched9.dta", clear
drop if no_block_match==1
drop no_block_match
duplicates t names_id, gen(dup1)

*Resolve names_id dupes, once and for all
sort names_id
egen sum_totp_hab=sum(totp_h9), by(names_id) 
gen pop_diff01_sum = abs(tot_p - sum_totp_hab)
gen pop_diff11_sum = abs(tot_p11 - sum_totp_hab)
*br names_id pca01_id h9v_id tot_p tot_p11 totp_h9 pop_diff01_sum pop_diff11_sum if dup1>0
count if dup1>0 & (pop_diff01_sum<=20 | pop_diff11_sum<=20 | pop_diff01_sum<=0.2*tot_p | pop_diff11_sum<=0.2*tot_p11)
count if dup1>0
drop if dup1>0 // 0.02% of names_ids, so easiest just to drop them, and they were all fuzzy merges


*Resolve h9v_id dupes, once and for all
duplicates t h9v_id, gen(dup2)	
sort h9v_id
*br if dup2>0

  // flag duplicates that are already in names_id
duplicates t h9v_id pca01_id, gen(dup_pca)
*br names_id pca01_id vd_id pca11_id h9v_id dup* if dup2>0 & dup2==dup_pca
gen names_collapse = ""
replace names_collapse = "pca dup" if dup2>0 & dup2==dup_pca & pca01_id!=.

  // flag duplicates where count_h9>1 and the total population is clsoe to adding up
*br names_id pca01_id vd_id pca11_id h9v_id tot_p tot_p11 totp_h sum_pop* count* names_collapse if dup2>0 & names_collapse==""
replace names_collapse = "hab split 01" if dup2>0 & names_collapse=="" & count_h9>1 & tot_p!=. & (abs(sum_pop_01_9-totp_h9)<20 | abs(sum_pop_01_9-totp_h9)<0.2*tot_p)
replace names_collapse = "hab split 11" if dup2>0 & names_collapse=="" & count_h9>1 & tot_p11!=. &(abs(sum_pop_11_9-totp_h9)<20 | abs(sum_pop_11_9-totp_h9)<0.2*tot_p11)

  // flag remaining duplicates with count_h9>1 as ones to split and check
replace names_collapse = "hab split ??" if dup2>0 & names_collapse=="" & count_h9>1

  // drop unsplittable h9v_id dups
drop if dup2>0 & names_collapse=="" & count_h9==1
gen row_id = _n

  // merge in uncollapsed hab09 dataset to try to split the dupes
joinby h9v_id using "$hab9/hab_census09_for_merge_all.dta", unmatched(master)
drop _merge block village 
replace totp = . if substr(names_collapse,1,9)!="hab split"
replace h9_id = . if substr(names_collapse,1,9)!="hab split"
*br names_id h9v_id tot_p tot_p11 totp_h sum_pop* count* names_collapse totp if dup2>0 & substr(names_collapse,1,9)=="hab split"

  // for 2-habitation villages, split evenly based on closest population
gen temp_diff01 = abs(totp - tot_p)
gen temp_diff11 = abs(totp - tot_p11)
egen temp_diff01_min1 = min(temp_diff01), by(names_id)
egen temp_diff11_min1 = min(temp_diff11), by(names_id)
egen temp_diff01_min2 = min(temp_diff01), by(h9_id)
egen temp_diff11_min2 = min(temp_diff11), by(h9_id)
gen hab_split_keep = ""
replace hab_split_keep = "keep 01"  if dup2==1 & temp_diff01==temp_diff01_min1 & temp_diff01==temp_diff01_min2 & names_collapse=="hab split 01" & count_h9==2 & tot_p!=. & hab_split_keep== ""
replace hab_split_keep = "keep 11"  if dup2==1 & temp_diff11==temp_diff11_min1 & temp_diff11==temp_diff11_min2 & names_collapse=="hab split 11" & count_h9==2 & tot_p11!=. & hab_split_keep==""
replace hab_split_keep = "keep 01?" if dup2==1 & temp_diff01==temp_diff01_min1 & temp_diff01==temp_diff01_min2 & names_collapse=="hab split ??" & count_h9==2 & tot_p!=. & hab_split_keep==""
replace hab_split_keep = "keep 11?" if dup2==1 & temp_diff11==temp_diff11_min1 & temp_diff11==temp_diff11_min2 & names_collapse=="hab split ??" & count_h9==2 & tot_p11!=. & hab_split_keep==""
gen keep_flag = (hab_split_keep!="")*h9_id
egen max_keep_flag1 = max(keep_flag), by(names_id)
egen min_keep_flag1 = min(keep_flag), by(names_id)
egen max_keep_flag2 = max(keep_flag), by(h9_id)
egen min_keep_flag2 = min(keep_flag), by(h9_id)
egen max_keep_flag3 = max(max_keep_flag1), by(h9v_id)
egen min_keep_flag3 = min(max_keep_flag1), by(h9v_id)
drop if dup2==1 & count_h9==2 & hab_split_keep=="" & max_keep_flag3>min_keep_flag3 & min_keep_flag3>0
replace hab_split_keep = "" if word(hab_split_keep,1)=="keep" & (max_keep_flag3==min_keep_flag3 | min_keep_flag3==0)
replace totp_h9 = totp if word(hab_split_keep,1)=="keep" 
replace maxp_h9 = totp if word(hab_split_keep,1)=="keep" 
replace count_h9 = 1 if word(hab_split_keep,1)=="keep"
gen h9_id_dupes = h9_id if word(hab_split_keep,1)=="keep"
drop sum_totp_hab pop_diff01_sum pop_diff11_sum dup_pca habitation scp stp totp h9_id temp_* *keep_flag* panchayat AP* 
duplicates drop
sort row_id
duplicates t row_id, gen(dup2b)
drop if dup2b==1 & substr(hab_split_keep,-1,1)=="?" & ((row_id==row_id[_n-1] & substr(hab_split_keep[_n-1],-1,1)!="?") | (row_id==row_id[_n+1] & substr(hab_split_keep[_n+1],-1,1)!="?"))
drop if dup2b==1 & ((row_id==row_id[_n-1] & abs(totp_h9-tot_p)>abs(totp_h9[_n-1]-tot_p[_n-1])) | (row_id==row_id[_n+1] & abs(totp_h9-tot_p)>abs(totp_h9[_n+1]-tot_p[_n+1])))
assert row_id==_n


  // for 2+habitation villages with 2+ dupes, find habitations that match village pop closely
duplicates t h9v_id if hab_split_keep=="" & names_collapse!="pca dup", gen(dup2c)	
replace dup2c = 0 if dup2c==.

joinby h9v_id using "$hab9/hab_census09_for_merge_all.dta", unmatched(master)
drop _merge block village 
replace totp = . if dup2c==0 
replace h9_id = . if dup2c==0

gen temp_diff01 = abs(totp - tot_p)
gen temp_diff11 = abs(totp - tot_p11)
egen temp_diff01_min1 = min(temp_diff01), by(names_id)
egen temp_diff11_min1 = min(temp_diff11), by(names_id)
egen temp_diff01_min2 = min(temp_diff01), by(h9_id)
egen temp_diff11_min2 = min(temp_diff11), by(h9_id)
/*replace hab_split_keep = "keep 01"  if dup2c>0 & temp_diff01==temp_diff01_min1 & temp_diff01==temp_diff01_min2 & names_collapse=="hab split 01" & count_h9>1 & tot_p!=. & hab_split_keep== ""
replace hab_split_keep = "keep 11"  if dup2c>0 & temp_diff11==temp_diff11_min1 & temp_diff11==temp_diff11_min2 & names_collapse=="hab split 11" & count_h9>1 & tot_p11!=. & hab_split_keep==""
replace hab_split_keep = "keep 01?" if dup2c>0 & temp_diff01==temp_diff01_min1 & temp_diff01==temp_diff01_min2 & names_collapse=="hab split ??" & count_h9>1 & tot_p!=. & hab_split_keep==""
replace hab_split_keep = "keep 11?" if dup2c>0 & temp_diff11==temp_diff11_min1 & temp_diff11==temp_diff11_min2 & names_collapse=="hab split ??" & count_h9>1 & tot_p11!=. & hab_split_keep==""
replace hab_split_keep = "keep ??"  if dup2c>0 & temp_diff11==temp_diff11_min1 & temp_diff11==temp_diff11_min2 & count_h9>1 & tot_p11!=. & hab_split_keep==""
gen keep_flag = (hab_split_keep!="")*h9_id
egen max_keep_flag1 = max(keep_flag), by(names_id)
egen min_keep_flag1 = min(keep_flag), by(names_id)
egen max_keep_flag2 = max(keep_flag), by(h9_id)
egen min_keep_flag2 = min(keep_flag), by(h9_id)
egen max_keep_flag3 = max(max_keep_flag1), by(h9v_id)
egen min_keep_flag3 = min(max_keep_flag1), by(h9v_id)
br names_id	h9v_id	tot_p	tot_p11	totp_h9	count_h9	names_collapse	hab_split_keep	dup2c	totp temp_diff01	temp_diff11	temp_diff01_min1	temp_diff11_min1	temp_diff01_min2	temp_diff11_min2	keep_flag	max_keep_flag1	min_keep_flag1	max_keep_flag2	min_keep_flag2	max_keep_flag3	min_keep_flag3 if dup2c>0
*/  
  // this is super tricky, because of the permutations of habitations and pca villages, so i'll only deal with those habitations that have 
	// exact population matches with pca village, since it's the singletons that we care about anyways.
*br names_id h9v_id tot_p tot_p11 totp_h9 count_h9 names_collapse hab_split_keep dup2c totp temp_diff01 temp_diff11 temp_diff01_min1 temp_diff11_min1 temp_diff01_min2 temp_diff11_min2 if dup2c>0 & (temp_diff01==0 | temp_diff11==0)
gen keep_flag = (dup2c>0 & (temp_diff01==0 | temp_diff11==0))*h9_id
egen max_keep_flag1 = max(keep_flag), by(names_id)
egen min_keep_flag1 = min(keep_flag), by(names_id)
egen max_keep_flag2 = max(keep_flag), by(h9_id)
egen min_keep_flag2 = min(keep_flag), by(h9_id)
egen max_keep_flag3 = max(max_keep_flag1), by(h9v_id)
egen min_keep_flag3 = min(max_keep_flag1), by(h9v_id)
sort keep_flag h9v_id
gen to_drop = 0
replace to_drop = 1 if keep_flag==keep_flag[_n-1] & keep_flag>0 & keep_flag!=.
replace to_drop = 1 if keep_flag==keep_flag[_n+1] & keep_flag>0 & keep_flag!=.
replace keep_flag = 0 if to_drop==1   // this gets rid of habitations matched to multiple villages
sort names_id keep_flag 
replace to_drop = 1 if names_id==names_id[_n-1] & names_id!=names_id[_n+1] & keep_flag>0 & keep_flag!=. & keep_flag[_n-1]>0 & keep_flag[_n-1]!=. & keep_flag[_n-1]<keep_flag
replace to_drop = 1 if names_id==names_id[_n-1] & names_id==names_id[_n+1] & names_id!=names_id[_n+2] & keep_flag>0 & keep_flag!=. & keep_flag[_n-1]>0 & keep_flag[_n-1]!=. & keep_flag[_n-1]<keep_flag
replace keep_flag = 0 if to_drop==1  // the next line gets rid of mutiple exact habitation matches within a village, which is almost certainly a duplicate (with slight variant) in the original habitation census
egen max_keep_flag4 = max(keep_flag), by(names_id)
assert keep_flag==0 | keep_flag==. | keep_flag==max_keep_flag4
replace totp_h9 = totp if keep_flag>0 & keep_flag!=.  
replace maxp_h9 = totp if keep_flag>0 & keep_flag!=. 
replace count_h9 = 1 if keep_flag>0 & keep_flag!=. 
replace h9_id_dupes = h9_id if keep_flag>0 & keep_flag!=. & h9_id_dupes==.
drop if max_keep_flag4>0 & max_keep_flag4!=. & keep_flag==0
drop habitation scp stp totp h9_id temp_* *keep_flag* to_drop panchayat AP*
duplicates drop
sort row_id
assert row_id==_n

  // drop remaining h9v_id dupes
duplicates t h9v_id if names_collapse!="pca dup" & h9_id_dupes==., gen(dup2d)
replace dup2d = 0 if dup2d==.
drop if dup2d>0
drop dup*
compress
save "$hmerge/pca_2001_names_merges_all_matched9_processed.dta", replace
}

********************************************************************************
********************************************************************************

** Step 18: Village matches: Combine 2003 and 2009 matches
{
use "$hmerge/pca_2001_names_merges_all.dta", clear
merge 1:1 names_id using "$hmerge/pca_2001_names_merges_all_matched3_processed.dta", nogen keepusing(block3 village3 totp_h3 maxp_h3 count_h3 h3v_id h3_id_dup)
merge 1:1 names_id using "$hmerge/pca_2001_names_merges_all_matched9_processed.dta", nogen keepusing(block9 village9 totp_h9 maxp_h9 count_h9 h9v_id h9_id_dup)
rename h3_id_dup h3_id
rename h9_id_dup h9_id

count if count_h3!=. & count_h9!=.
count if count_h3!=. | count_h9!=.
count if count_h3==1 & count_h9==1
count if count_h3==1 | count_h9==1
count if count_h3==1
count if count_h9==1
count if count_h3==1 & count_h9!=. & count_h9!=1
count if count_h9==1 & count_h3!=. & count_h3!=1

correlate count_h3 count_h9 if count_h3!=. & count_h9!=. // luckily, very high correlation!

gen match_h3 = h3v_id!=.
gen match_h9 = h9v_id!=.
gen sing_h = count_h3==1 | count_h9==1  // create unified singleton dummy
replace sing_h = . if match_h3==0 & match_h9==0  // leave blank for non-matches 

compress
save "$hmerge/pca_2001_names_all_merged.dta", replace


*Prep for merge into final dataset
use "$hmerge/pca_2001_names_all_merged.dta", clear
rename bk_code_pca bk_code

  // merge in DPR codes and plans from summary doc (as alternative to RGGVY microdata)
merge m:m st_code dt_code using "$rggvy/rggvy_district_progress_X_XI_processed.dta", nogen keep(1 3) keepusing(vdpr4 vplan4)

drop if pca01_id==. // we need pre and post data for all villages, can maybe add some back in if pressed for power
duplicates drop pca01_id pca11_id, force

  // collapse 2001 duplicates
duplicates t pca01_id, gen (dup_pca)
collapse (min) names_id h3v_id h3_id h9v_id h9_id v_id_master ///
         (mean) totp_h3 count_h3 totp_h9 count_h9 vplan* vdpr*  /// 
         (max) match* sing_h maxp_h3 maxp_h9 dup_pca, by(st_code dt_code bk_code vi_code pca01_id) fast				 
order names_id pca01_id st_code-vi_code dup_pca vplan* vdpr* match* sing_h *3* *9*
compress

la var names_id "Habitation merge village identifier"
la var pca01_id "2001 PCA id"
la var st_code "2001 state code"
la var dt_code "2001 district code"
la var bk_code "2001 block code"
la var vi_code "2001 village code"
la var dup_pca "2011 duplicates after habitation merge"
la var vplan "RGGVY virtual plan 1 (based on RGGVY microdata, block-level averaged)"
la var vplan2 "RGGVY virtual plan 2 (based on RGGVY microdata, block-level mode)"
la var vplan3 "RGGVY virtual plan 3 (based on RGGVY microdata, district-level averaged)"
la var vplan4 "RGGVY virtual plan 4 (based on RGGVY district-level data)"
la var vdpr "RGGVY virtual dpr 1 (based on RGGVY microdata, block-level averaged)"
la var vdpr2 "RGGVY virtual dpr 2 (based on RGGVY microdata, block-level mode)"
la var vdpr3 "RGGVY virtual dpr 3 (based on RGGVY microdata, district-level averaged)"
la var vdpr4 "RGGVY virtual dpr 4 (based on RGGVY district-level data)"
la var match_h3 "Dummy for matching into 2003 Hab Census"
la var match_h9 "Dummy for matching into 2009 Hab Census"
la var sing_h "Dummy for single-habitation villages"
la var h3v_id "2003 Hab Census village id"
la var h3_id "2003 Hab Census habitation id"
la var totp_h3 "2003 Hab Census implied village population (summing hab pops)"
la var count_h3 "2003 Hab Census number of habitations per village (avg for dup matches)"
la var maxp_h3 "2003 Hab Census pop of largest habitation in village"
la var h9v_id "2009 Hab Census village id"
la var h9_id "2009 Hab Census habitation id"
la var totp_h9 "2009 Hab Census implied village population (summing hab pops)"
la var count_h9 "2009 Hab Census number of habitations per village (avg for dup matches)"
la var maxp_h9 "2009 Hab Census pop of largest habitation in village"
la var v_id_master "RGGVY microdata village id"

save "$panel/pca_2001_names_hab_merge_final.dta", replace

}

********************************************************************************
********************************************************************************
